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
🌐
GitHub
github.com › airbnb › javascript › issues › 1718
Nested ternaries aren't bad, just misused · Issue #1718 · airbnb/javascript
November 11, 2017 - With this pattern, it's impossible to get lost in nested contexts. Once you've ruled out a line applying to the case you're considering, you can safely forget it. It's easier to understand than the solutions provided by the style guide right now, and also saves a memory allocation. Here are some other examples where this pattern improves code: // bad let result; if (operator === ‘+’) { result = left + right; } else if (operator === ‘*’) { result = left * right; } else if (operator === ‘-’) { result = left — right; } else { result = left / right; } // good const result = operator === ‘+’ ?
Published   Feb 01, 2018
🌐
GitHub
gist.github.com › marufMunshi › 538f5e68c87fdf84a077887f377d29f3
nested ternary operator · GitHub
nested ternary operator · Raw · nested-ternary-operator.jsx · This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
The ternary operator is right-associative, which means it can be "chained" in the following way, similar to an if … else if … else if …
🌐
GitHub
github.com › rubocop › rubocop › issues › 11008
Style/NestedTernaryOperator should be able to `AllowElseIf: true` · Issue #11008 · rubocop/rubocop
But there are times when I think the nested ternary is succinct and to-the-point in a way that works very well for my team and probably others. Specifically, when the negative clause contains another ternary expression, that is analogous to if/elsif/else. We usually don't interpret if/elsif/else as "nested", although it logically is.
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › nested ternary operator javascript | example code
Nested ternary operator JavaScript | Example code
November 22, 2022 - You can nest one ternary operator as an expression inside another ternary operator to work as a Nested ternary operator in JavaScript.
🌐
Medium
medium.com › javascript-scene › nested-ternaries-are-great-361bddd0f340
Nested Ternaries are Great. Note: This is part of the “Composing… | by Eric Elliott | JavaScript Scene | Medium
February 28, 2018 - Most functional programming languages use ternary expressions for their if keyword. Why? An expression is a chunk of code that evaluates to a single value. A statement is a chunk of code that may not evaluate to a value at all. In JavaScript if statements don’t evaluate to values.
🌐
GitHub
github.com › prettier › prettier › issues › 9561
Proposals for nested ternary formatting · Issue #9561 · prettier/prettier
November 1, 2020 - Unless we were to shift to using it for ternaries everywhere, I think we should not adopt Option A. Personally, I actually quite like Option B2, proposed by @roryokane below, with a prior implementation by @zeorin in #4767, and I think both Option B and Option B2 warrant further exploration. ... lang:javascriptIssues affecting JSIssues affecting JSstatus:needs discussionIssues needing discussion and a decision to be made before action can be takenIssues needing discussion and a decision to be made before action can be taken
Published   Nov 01, 2020
🌐
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 - We can nest a ternary operator as an expression inside another ternary operator. We can use this to replace if…else if…else statements and switch statements. For example, we could have a piece of code that sets the English word for the numbers ...
🌐
DEV Community
dev.to › vitiok78 › comment › 1ha52
Nested ternary operator is a pure evil. Just. Don't. Use. It. Never. Thank me... - DEV Community
Totally agree here, please don't use ternary operator if it's going to have 2 levels. Next de will have to give more time to understand it. Just use, if else.. no harm in that.. But that being said, a great article bro. Few shortcodes are really helpful.👍 ... Started as tech enthusiast, later developed keen interest in programming. I have always been interested in learning new technical things, solving problems. ... I've never seen any problem at all with using nested ternary operators especially in React component code.
🌐
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:
🌐
Rithm School
rithmschool.com › using-the-javascript-ternary-operator
Mastering the JavaScript Ternary Operator: Effective Usage
May 22, 2023 - If you’re just getting used to ternary operators in JavaScript, you might have the urge to use them as much as possible. But a simple rule that I have for student’s code as well as my own code is not to make your ternary operators too complex. For example, you should not try to make ternary operators nested ...
🌐
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 - In this comprehensive guide, we'll explore the ins and outs of the JavaScript ternary operator. We'll start with the basics, explaining its syntax and how it compares to traditional if...else statements. We'll then delve into more advanced topics like nested ternary operators, common pitfalls to avoid, and best practices to follow.
🌐
GitHub
github.com › topics › ternary-operator
ternary-operator · GitHub Topics · GitHub
javascript conditional-statements ternary-operator nested-ternaries
🌐
Accreditly
accreditly.io › articles › dont-nest-ternary-operators-please
Don't nest ternary operators. Please! - Accreditly
December 23, 2025 - While ternary operators are a useful feature of JavaScript, nesting them can lead to code that's hard to read, debug, and maintain. Opting for clearer, more straightforward alternatives like if-else statements, switch statements, or function ...
🌐
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.