The main readability issue with ternaries is the lack of grouping symbols compared to if/else statements. If you use parentheses to group sub-expressions and don’t get too excessive with them I don’t see why it should be a readability concern. After all “?” is a direct replacement for “if” and “:” is a direct replacement for “else”, the only difference between the two notations is the placement of the if and the need for return statements. Answer from Quantum-Bot on reddit.com
🌐
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:
🌐
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.
🌐
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:
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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 ...
🌐
Tutorial Teacher
tutorialsteacher.com › csharp › csharp-ternary-operator
C# ?: Ternary Operator (Conditional Operator)
Example: Ternary operator replaces if statement Copy · int x = 10, y = 100; if (x > y) Console.WriteLine("x is greater than y"); else Console.WriteLine("x is less than y"); ... Nested ternary operators are possible by including a conditional ...
🌐
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 ...
🌐
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.
🌐
Reddit
reddit.com › r/learnjavascript › nested ternary operators!!
r/learnjavascript on Reddit: Nested ternary operators!!
January 19, 2025 -

Hi

I never use ternary operators, but I'm willing to learn and give it a shot for my next personal project.

My question is, i confused myself now trying to do a nested ternary operator.

For example a typical ifelse nested statement looks like this

if (example1 === 1) {

if (example2 === 2) {

   console.log("yes")

    }

 else if (example2 === 3 {

     console.log("no")

    }

  else {

    return example3

   }

else if (example2 === 2) {

if (example 3 === 3) {

   console.log("yes")      

  }

else {

   return example3

  }

else {

console.log ("i know this example suck")

}

how do i do the exact nesting in ternary operators, i googled but got more confused.

🌐
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.
🌐
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 ...
🌐
Codecademy
codecademy.com › forum_questions › 50a735d192bf860b2000077c
Ternary operator more than one condition | Codecademy
Now let’s look at the question example: var myAge = 18; var reps = [ "You are younger than 18 years old", "You are older than 18 years old", "You are 18 years old" ]; var respond = myAge<18 ? reps[0] : (myAge>18 ? reps[1] : reps[2]); console.log(respond); The running code is in the lab: sample nesting in ternary expressisons