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 OverflowMedium
medium.com › codingbeauty-tutorials › javascript-nested-ternary-operator-dc28551fb8c3
How to Use the JavaScript Nested Ternary Operator | Coding Beauty Tutorials
August 1, 2023 - We can nest a ternary operator as an expression inside another ternary operator.
Top answer 1 of 10
55
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 ;
2 of 10
42
I think you can have this to avoid the deep nesting:
var H
if(C == 0){
H = null;
}
else if(V == r){
H = (g - b) / C;
}
else if (V == g){
H = (b - r) / C + 2;
}
else {
H = (r - g) / C + 4;
}
Videos
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
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.
Top answer 1 of 5
3
Statements , like return ... (the "return statement"), cannot be placed inside ternary operators — only expressions can be placed inside the ternary operator. Therefore, this code cannot be changed to use ternary operators.
2 of 5
1
Your code was messed up, so I took some guesses to fix it. 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"); } You can have expressions (but not returns) within a ternary. A ternary expression returns a value, so is generally used to return a value with some conditional branches, as one statement. In keeping with that, I'll change the if statement version to this: var result; if (example1 === 1) { if (example2 === 2) { result = "yes"; } else if (example2 === 3) { result = "no"; } else { return example3; } } else if (example2 === 2) { if (example 3 === 3) { result = "yes2"; } else { return example3; } } else { result = "final"; } You can replace features of an if statement with the equivalent from a ternary. And you can use newlines just fine, and even brackets to help you visualise the structure. ...
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
gist.github.com › mjackson › 8b380f12df309aa00d0f278b1f0a5119
Nested ternaries work like if/else if/else for JSX · GitHub
Nested ternaries work like if/else if/else for JSX - nested-ternary-jsx.js
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.
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
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.
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.
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.