To my personal taste, a carefully aligned nested ternary beats the if-else mess:

const H =
  C == 0 ? null            :
  V == r ? (g - b) / C     :
  V == g ? (b - r) / C + 2 :
           (r - g) / C + 4 ;
Answer from Andrey Mikhaylov - lolmaus on Stack Overflow
🌐
Reddit
reddit.com › r/react › nested ternary operators. how bad are they?
r/react on Reddit: Nested ternary operators. How bad are they?
January 26, 2024 -

So I saw an article recently that was talking about minimizing the use of ternary operators where possible and reflecting on my own use of them especially in JSX, I have a few questions...

Before I get decided to post my questions, I checked React subs and most discussions on this are a couple years old at least and I thought perhaps views have changed.

Questions:

  1. Is the main issue with using nested ternary operators readability?

I have found myself using ternary operators more and more lately and I even have my own way of formatting them to make them more readable. For example,

            info.type === "playlist"
            ?   info.creationDate
                ?   <div className="lt-info-stats">
                        <span className="text pure">Created on {info.creationDate}</span>
                    </div>
                :   null
            :   info.type === "artist"
                ?   <div className="lt-info-stats">
                        <span className="text pure">{info.genre}</span>
                    </div>
                :   <div className="lt-info-stats">
                        <span className="text pure">{info.releaseDate}</span>
                        <span className="cdot" style={{ fontWeight: "bold", margin: "1px" }}>·</span>
                        <span className="text pure">{info.genre}</span>
                    </div>

When written like this, I can visually see the blocks and tell them apart and it looks a lot like how an if/else might look.

nested ternary operator formatting

2. What is the preferred formatting of ternary operators in general and what do you think should be done to make them more readable?

3. How do people feel about nested ternary operators today? How big of a nono is it to have them in code (if it is a nono)?

I would love you know peoples thoughts on ternary operators in React in general as well.

Thanks for your attention!

🌐
Medium
medium.com › codingbeauty-tutorials › javascript-nested-ternary-operator-dc28551fb8c3
How to Use the JavaScript Nested Ternary Operator | Coding Beauty Tutorials
August 1, 2023 - For example, we could have a function that returns “yes” or “no” depending on whether a number passed to it is odd or not. function isOdd(num) { if (num % 2 === 1) return 'yes'; else return 'no'; } We can refactor the isOdd function ...
🌐
Medium
medium.com › javascript-scene › nested-ternaries-are-great-361bddd0f340
Nested Ternaries are Great
February 28, 2018 - The common claim I hear is that nested ternary expressions are “hard to read”. Let’s shatter that myth with some code examples:
🌐
Sonar
sonarsource.com › blog › stop-nesting-ternaries-javascript
Stop nesting ternaries in JavaScript | Sonar
December 7, 2023 - There are no if/else statements in JSX, so you need to use the ternary operator to make decisions. It is then common to nest those operations when rendering components conditionally and including conditional attributes in code like this:
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › c-nested-ternary-operator
C++ | Nested Ternary Operator - GeeksforGeeks
July 11, 2025 - 2. a ? b: c ? d : e ? f : g ? h : i =>This Nested ternary operator can be broken into if, else and else-if statement.
🌐
Codespindle
codespindle.com › Java › Java_Ternary.html
Ternary Operator and Nested Ternary Operators in Java
The following example shows how to use the ternary operator to assign the value "true" to the variable isAdult if the user's age is greater than or equal to 18, and the value "false" otherwise: Java · int age = 20; boolean isAdult = (age >= 18) ? true : false; System.out.println(isAdult); // Prints "true" The ternary operator can be nested to create more complex conditional statements.
🌐
Codecademy
codecademy.com › forum_questions › 4f2343035ed3e90001012641
Can you create a nested ternary operator? | Codecademy
I always prefer using if statements, they have a nice structure to them and are very intuitive if you use {} and indent each nested if. ... it’s also good to space out the binary operators like necro did. I actually space out after the parenthesis too. if ( i < n ) { etc etc ... I am learning JavaScript right now, but if my memory serves correctly for C# you cannot use ternary operators for nested if statments.
Find elsewhere
🌐
Plain English
plainenglish.io › home › blog › javascript › how to use the javascript nested ternary operator
How to Use the JavaScript Nested Ternary Operator
May 17, 2022 - For example, we could have a function that returns “yes” or “no” depending on whether a number passed to it is odd or not. function isOdd(num) { if (num % 2 === 1) return 'yes'; else return 'no'; } We can refactor the isOdd function ...
🌐
LabEx
labex.io › tutorials › java-how-to-nest-the-ternary-operator-for-complex-conditional-statements-in-java-417399
How to nest the ternary operator for complex conditional statements in Java | LabEx
In this example, the first condition checks if the age is less than 18. If true, it returns "Minor". If false, it checks if the age is less than 65. If true, it returns "Adult". If both conditions are false, it returns "Senior". Nesting ternary operators can be a powerful tool for handling complex conditional logic in a concise and readable way.
🌐
Sub-Etha Software
subethasoftware.com › 2016 › 12 › 09 › nested-ternary-operators-in-c
Nested ternary operators in C | Sub-Etha Software
June 4, 2024 - For instance, turning a boolean true/false result in to a string: printf( "Status: %s\n", (status == true ? "Enabled" : "Disabled" ); This will print “Status: Enabled” if status==true, or “Status: Disabled” if status==false.
🌐
Tutorial Teacher
tutorialsteacher.com › csharp › csharp-ternary-operator
C# ?: Ternary Operator (Conditional Operator)
Example: Nested ?: Copy · int x = 10, y = 100; string result = x > y ? "x is greater than y" : x < y ? "x is less than y" : x == y ? "x is equal to y" : "No result"; Console.WriteLine(result); Try it · The ternary operator is right-associative. The expression a ?
🌐
freeCodeCamp
freecodecamp.org › news › c-ternary-operator
Ternary Operator in C Explained
January 20, 2020 - Here’s the above example rewritten to use the ternary operator: int a = 10, b = 20, c; c = (a < b) ? a : b; printf("%d", c); ... Remember that the arguments value_if_true and value_if_false must be of the same type, and they must be simple ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Ternary-Operator-Nested-Examples-Return-if-else-symbol-void-null
How to use Java's conditional operator ?:
To use the Java ternary operator, follow these steps: In round brackets, provide a condition that evaluates to true or false. Place a question mark after the round brackets. After the question mark, state the value to return if the condition is true. Add a colon. After the colon, specify the value to return if the condition is false. Here is a simple example of the Java ternary operator in action:
🌐
DataCamp
datacamp.com › tutorial › pythons-ternary-operators-guide
Python's Ternary Operators Guide: Boosting Code Efficiency | DataCamp
May 17, 2024 - The nested ternary operator in Python allows you to combine multiple conditional expressions within a single ternary operator. This can be particularly useful when you need to evaluate multiple conditions and return different results based on each condition. Let's consider an example where we want to determine the category of a given number based on its value:
🌐
Accreditly
accreditly.io › articles › dont-nest-ternary-operators-please
Don't nest ternary operators. Please! - Accreditly
December 23, 2025 - It takes three operands: a condition, a result upon the condition being true, and a result if it's false. ... Nesting ternary operators means using one ternary inside another.
🌐
Codecademy
codecademy.com › forum_questions › 50a735d192bf860b2000077c
Ternary operator more than one condition | Codecademy
Ternary stands for three. So when creating a ternary we can block it out in three parts: variable = expression part ? true function : false function; Nesting can be done in one or both of the true/false functions, so long as we give a function to each nested expression outcome:
🌐
Wiingy
wiingy.com › home › learn › python › ternary operators in python
Ternary Operators in Python
January 30, 2025 - In this example, we have used a nested ternary operator to check if age is a child, adult, or senior citizen. If age is less than 18, then the value “Child” is returned. If it is greater than or equal to 18 and less than 65, then the value ...
🌐
freeCodeCamp
freecodecamp.org › news › the-ternary-operator-in-javascript
JavaScript Ternary Operator – Syntax and Example Use Case
January 6, 2023 - What if you wanted to achieve an if...else if...else statement with a ternary operator? Then you can use nested ternary operators.