Yes you can go wild nesting ternaries. I find this version to be fairly readable:

var foo = (
  bar === 'a' ? 1 : // if 
  bar === 'b' ? 2 : // else if 
  bar === 'c' ? 3 : // else if
  null // else 
);

but that's not a widely shared opinion, and you should probably stick to if/else or switch when working on a team.

Answer from Damon on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse. ... const greeting = (person) => { const name = person ? person.name : "stranger"; return `Howdy, ${name}`; }; console.log(greeting({ name: "Alice" })); // "Howdy, Alice" console.log(greeting(null)); // "Howdy, stranger" The ternary operator is right-associative, which means it can be "chained" in the following way, similar to an if …
Discussions

Multiple Conditional (Ternary) Operators
Tell us what’s happening: It works with and without parentheses around num > 0 , whats the difference. Examples online are also mostly without parentheses · In this specific example, nothing. You’ve got a simple boolean condition before the question mark, which is exactly what the ternary ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
1
November 24, 2021
Nested ternary operators. How bad are they?
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. More on reddit.com
🌐 r/react
115
92
January 26, 2024
Can ternary operators contain multiple actions?
Can ternary operators contain multiple actions · It is very-very important to note that : Can we use multiple actions in truthy/falsy part · Yes, we can, but with one proviso… There is no block demarcation so that action should be simple, else it would be better to abstract it away in a ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
15
March 21, 2019
Javascript ternary operator syntax with object
The ternary operator needs to have an else clause that tells it what to do if the condition is false. More on reddit.com
🌐 r/learnprogramming
10
4
September 9, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-use-multiple-ternary-operators-in-a-single-statement-in-javascript
How to use multiple ternary operators in a single statement in JavaScript ? - GeeksforGeeks
September 19, 2024 - In this example, we are checking two conditions and using nested ternary operators to return the corresponding expression based on the conditions: ... const age = 45; (age > 30) ? (age > 70) ? console.log("You are getting old") : console.log("You are between 30 and 69") : console.log("You are below 30"); ... Let's look at another example where we use multiple ternary operators to determine a grade based on a score:
🌐
freeCodeCamp
freecodecamp.org › news › the-ternary-operator-in-javascript
JavaScript Ternary Operator – Syntax and Example Use Case
January 6, 2023 - The ternary operator is a conditional operator which evaluates either of two expressions – a true expression and a false expression – based on a conditional expression that you provide.
🌐
YouTube
youtube.com › watch
JavaScript Basic 111: Use Multiple Conditional (Ternary) Operators | FreeCodeCamp - YouTube
🎬 **"Ternary Tango: Multiple Moves, One Groovy Dance!"** 🔗Lesson Link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-j...
Published   September 5, 2023
🌐
Codecademy
codecademy.com › forum_questions › 50a735d192bf860b2000077c
Ternary operator more than one condition | Codecademy
I understand the ternary operator for the conditional statement is as shown below: var Name = "Mike"; if (Name === "Mike" { ...
Find elsewhere
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how to use the ternary operator in javascript
Quick Tip: How to Use the Ternary Operator in JavaScript — SitePoint
November 6, 2024 - The ternary operator can be used for value assignment, executing expressions based on a condition, and checking if a variable is null or undefined. It can also handle multiple conditions, similar to if…else if…else statements, by nesting or chaining conditions within the operator’s expressions.
🌐
JavaScript in Plain English
javascript.plainenglish.io › mastering-ternary-operators-in-javascript-how-to-use-multiple-in-a-single-statement-65e5c6c01c7f
Mastering Ternary Operators in JavaScript: How to Use Multiple in a Single Statement | by PRADIP KAITY | JavaScript in Plain English
May 27, 2025 - One of the most concise and elegant ways to express conditional statements is by using the ternary operator. It allows you to write simple if-else conditions in a single line of code. But did you know that you can use multiple ternary operators in a single statement?
🌐
Flexiple
flexiple.com › javascript › javascript-ternary
JavaScript Ternary Operator – Syntax and Example Use Case - Flexiple
April 16, 2024 - It's particularly useful for ... ternary operators in JavaScript, one ternary operation is placed inside another, allowing for multiple conditions to be evaluated in a concise manner....
🌐
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!

🌐
Lenovo
lenovo.com › home
Ternary Operator: When to Use the Ternary Operator & Why is it Useful? | Lenovo NZ
No, the ternary operator only evaluates a single condition. If multiple conditions need to be checked, logical operators (e.g., && and ||) or nested ternary operators can be used to combine the conditions.
🌐
DEV Community
dev.to › wizdomtek › mastering-the-javascript-ternary-operator-a-comprehensive-guide-5388
Mastering the JavaScript Ternary Operator: A Comprehensive Guide - DEV Community
October 27, 2024 - Maintain Readability: If the ternary operator makes the code less readable, opt for an if...else statement. The condition is simple and straightforward. You want to assign a value based on a condition. It enhances code conciseness without sacrificing readability. Dealing with complex conditions or multiple nested conditions. The expressions involve side effects or multiple statements. Readability is compromised. The JavaScript ...
🌐
Refine
refine.dev › blog › javascript-ternary-operator
How to Use the JavaScript Ternary Operator | Refine
October 8, 2024 - Though it’s not always the most readable, you can chain ternary operators to handle multiple conditions. It works like an inline if/else if/else block. Here’s an example: const grade = score > 90 ?
🌐
Stack Abuse
stackabuse.com › guide-to-the-ternary-operator-in-javascript
Guide to the Ternary Operator in JavaScript
May 30, 2022 - Let's rewrite this if-else block using ternary operator : let age = 21; let result = age >= 20 ? "User can view content" : "User cannot view content"; console.log(result); Basically, anything before the question mark (?) is the condition we are checking. The other two operands are expressions, the first one before the semi-colon (:) and the second after.
🌐
KoderHQ
koderhq.com › tutorial › javascript › conditional-control
Javascript if, else if, else (conditional control) Tutorial | KoderHQ
In this Javascript tutorial we learn how to use if, else if and else statements to control the flow of our applications conditionally. We cover the if statement, the else if ladder and the else catch all fallback, as well as the shorthand ternary operator ( ? : ). We also cover how to evaluate multiple conditions inside a single conditional statement, and how to nest conditionals inside each other.
🌐
Codecademy Forums
discuss.codecademy.com › web development
Can ternary operators contain multiple actions? - Web Development - Codecademy Forums
March 21, 2019 - It is very-very important to note that : Can we use multiple actions in truthy/falsy part? eg. if (condition) { action-1; action-2; … } else { action-n; //n th action action-(n+1); // n+1 th action … } in te…
🌐
RunJS
runjs.app › blog › using-the-ternary-operator-in-javascript
Using the Ternary Operator in JavaScript
October 14, 2022 - Ternary operators can be nested to check multiple conditions, similar to an if...else if...else statement.
🌐
Medium
medium.com › geekculture › how-to-execute-multiple-actions-in-a-ternary-block-php-38548d0236c6
How to Execute Multiple Actions in a Ternary Block — PHP | by Simon Ugorji | Geek Culture | Medium
December 24, 2021 - But it doesn’t work and I will prefer you use ternary operators for little code logics and use the native if/else statements for complex logic. This is unlike what’s made available in JavaScript. Only use Ternary operators for a single condition ...