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 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
W3Schools
w3schools.io › javascript › es11-optional-chaining
ES2020(ES11) - Javascript Optional Chaining Operator - w3schools
This tutorial covers the latest javascript features, Optional Chaining Operator released in 2020. ES11 or ES2020 or EcmaScript2020 .. This tutorials explains about fixing an error , an feature of Optional Chaining Operator. TypeError: Cannot read property ‘’ of undefined · A nested object is an object nested inside an object with a deep level.
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-ternary-operator
JavaScript Ternary Operator - GeeksforGeeks
The ternary operator can be nested, allowing you to perform multiple conditional checks in a single line of code.
Published June 24, 2019
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 …
EyeHunts
tutorial.eyehunts.com › home › nested ternary operator javascript | example code
Nested ternary operator JavaScript | Example code
November 22, 2022 - <!DOCTYPE html> <html> <body> <script type="text/javascript"> let n = 100; let result = (n >= 0) ? (n == 0 ? "zero" : "positive") : "negative"; console.log(`The number is ${result}.`); </script> </body> </html> ... Note: It’s hard to read nested ternary operators, You should try to avoid it.
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.
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. ...
W3Schools
w3schools.com › react › react_es6_ternary.asp
React ES6 Ternary Operator
The ternary operator is a simplified conditional operator like if / else. Syntax: condition ? <expression if true> : <expression if false> ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an ...
Nickmccullum
nickmccullum.com › javascript-ternary-operator
How to Use the Ternary Operator in JavaScript | Nick McCullum
This tutorial will teach you everything you need to know about the ternary operator in JavaScript. We'll cover it's syntax, implementation, and advanced functionality like nested ternary operators and multiple operations.
Programiz
programiz.com › javascript › ternary-operator
JavaScript Ternary Operator (with Examples)
In this tutorial, you will learn about the conditional/ternary operator in JavaScript with the help of examples.
Codecademy
codecademy.com › forum_questions › 4f4bdf2a90aee1000300045d
5. Nested ternaries | Codecademy
I tried doing “the exact same thing” with another nested ternary, like this: Wob: function(a,b) { return a===b? 0 : a>b? (this.Wob(a--,b)+1) : (this.Wob(a,b--)+1); }, ... Is there a recursion limit when using ternary operators?
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.
Rithm School
rithmschool.com › using-the-javascript-ternary-operator
Mastering the JavaScript Ternary Operator: Effective Usage
May 22, 2023 - Let’s talk about the ternary operator and discuss some rules of thumb for how to use it. The JavaScript ternary operator is a single line control flow statement. You can think of it like a mini if/else statement. It contains 3 parts: the conditional, the truthy statement, and the falsy statement.
Scaler
scaler.com › home › topics › javascript › javascript ternary operator
JavaScript Ternary Operator - Scaler Topics
February 19, 2024 - The expression consists of three operands: the condition, value if true, and value if false. The evaluation of the condition should result in either a true/false or a boolean value.