267

I came across the following line

hsb.s = max != 0 ? 255 * delta / max : 0;

What do the ? and : mean in this context?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Inaimathi
  • 13,853
  • 9
  • 49
  • 93
  • 1
    Does this answer your question? [How do you use the ? : (conditional) operator in JavaScript?](https://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript) – Donald Duck Jan 27 '23 at 16:40

8 Answers8

376

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;
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Greg
  • 23,155
  • 11
  • 57
  • 79
  • 29
    "?" 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.... – Jason S Nov 20 '09 at 17:11
  • 11
    Ok, ok... now I'm using an ambiguous pronoun, happy? :) – Greg Nov 20 '09 at 17:16
  • 16
    To 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). – Davy8 Aug 15 '11 at 18:56
  • 7
    @Davy8: And this one can be called the [tag:conditional-operator] to be specific. – Mechanical snail Aug 14 '12 at 01:02
  • What if you wanted to check two conditions? – Thanos Dodd Apr 12 '20 at 07:49
  • @ThanosDodd - Use nesting, or use `if`/`else if`/`else` instead. – T.J. Crowder Jan 19 '21 at 17:20
40

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
Jason S
  • 184,598
  • 164
  • 608
  • 970
7

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.

Nikolas Stephan
  • 1,280
  • 12
  • 10
7
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;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
1

?: 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
}

MDN - Conditional (Ternary) Operator

0

? : isn't this the ternary operator?

var x= expression ? true:false

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • 3
    That'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. – Scott Lahteine Jan 04 '12 at 23:15
0

What you are referring to is called a ternary operator, it is essentially a basic if condition check that can be written to execute an operation if the block of code within the ternary operation is valid, otherwise default to a fallback.

A ternary operation is written in the following syntax:

condition ? exprIfTrue : exprIfFalse
  • condition An expression whose value is used as a condition.
  • exprIfTrue An expression which is evaluated if the condition evaluates to a truthy value (one which equals or can be converted to true).
  • exprIfFalse An expression which is executed if the condition is falsy (that is, has a value which can be converted to false).

Example

Take the given function below which should return the string Yes if the number provided to the function is even, otherwise return No.

function isEven(num) {
    return (num % 2 == 0) ? "Yes" : "No";
}

console.log("2: " + isEven(2));
console.log("3: " + isEven(3));

Explanation

The operation above broken down:

  • (num % 2 == 0) | This is a simple if statement condition to check if the expression within the brackets is true.
  • ? "Yes" If the operation is true, the string literal given is automatically returned as a result of this execution.
  • : "No" This is the else clause in this operation, if the condition is not met then No is returned.
Skully
  • 2,882
  • 3
  • 20
  • 31
-4

Be careful with this. A -1 evaluates to true although -1 != true and -1 != false. Trust me, I've seen it happen.

so

-1 ? "true side" : "false side"

evaluates to "true side"

MEB
  • 53
  • 6
  • 5
    "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. (https://developer.mozilla.org/en-US/docs/Glossary/Truthy) – jobmo Sep 29 '17 at 10:29