Inaimathi Inaimathi - 1 year ago 153
Javascript Question

JavaScript Question Mark Operator

I came across the following line:

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




What are the
?
and
:
, are they operators? What are their purpose?


For further knowledge, the code was part of an RGB to HSB color converter

Answer Source

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;