Question mark and colon in JavaScript
Solution 1
It is called the Conditional Operator (which is a ternary operator).
It has the form of: condition
? value-if-true
: value-if-false
Think of the ?
as "then" and :
as "else".
Your code is equivalent to
if (max != 0)
hsb.s = 255 * delta / max;
else
hsb.s = 0;
Solution 2
Properly parenthesized for clarity, it is
hsb.s = (max != 0) ? (255 * delta / max) : 0;
meaning return either
-
255*delta/max
if max != 0 -
0
if max == 0
Solution 3
hsb.s = max != 0 ? 255 * delta / max : 0;
?
is a ternary operator. It works like an if
in conjunction with the :
!=
means not equals
So, the long form of this line would be
if (max != 0) { //if max is not zero
hsb.s = 255 * delta / max;
} else {
hsb.s = 0;
}
Solution 4
This is probably a bit clearer when written with brackets as follows:
hsb.s = (max != 0) ? (255 * delta / max) : 0;
What it does is evaluate the part in the first brackets. If the result is true then the part after the ? and before the : is returned. If it is false, then what follows the : is returned.
Solution 5
?: is a short-hand condition for else {}
and if(){}
problems.
So your code is interchangeable to this:
if(max != 0){
hsb.s = 225 * delta / max
}
else {
hsb.s = 0
}
Related videos on Youtube
Inaimathi
Common Lisp/Haskell/JavaScript/Scheme/Elisp/Ruby hacker with a degree in Graphic Design, an eye for layout and a hungry, hungry mind.
Updated on November 30, 2021Comments
-
Inaimathi over 2 years
I came across the following line
hsb.s = max != 0 ? 255 * delta / max : 0;
What do the
?
and:
mean in this context? -
Jason S over 14 years"?" isn't the ternary operator; "? :" is the ternary operator. Talking about "?" as the ternary operator is like talking about Abbott without Costello, Laurel without Hardy, Cheech without Chong....
-
Greg over 14 yearsOk, ok... now I'm using an ambiguous pronoun, happy? :)
-
Davy8 over 12 yearsTo be pedantic, it's a ternary operator, which happens to be the only one in most programming languages. Any operator that works on 3 parts is a ternary operator, just like
addition
is a binary operator that operates on the preceding and following expressions (e.g. 1+2 the plus operates on 1 and 2), or negation is a unary operator (e.g. -x where the value of x is negated). -
Scott Lahteine over 12 yearsThat's one example of its use, but there's actually a shorter version of your statement, for those cases where you just want TRUE / FALSE: If 'expression' was just some variable with a number or string in it, "var x = !!expression" will make it into a boolean result.
-
Mechanical snail over 11 years@Davy8: And this one can be called the conditional-operator to be specific.
-
jobmo over 6 years"In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN)." This is why -1 is evaluated as true. (developer.mozilla.org/en-US/docs/Glossary/Truthy)
-
Thanos Dodd about 4 yearsWhat if you wanted to check two conditions?
-
T.J. Crowder over 3 years@ThanosDodd - Use nesting, or use
if
/else if
/else
instead.