Conditional Operator in JavaScript
In JavaScript, you may encounter a statement like this:
hsb.s = max != 0 ? 255 * delta / max : 0;
This line exemplifies the use of the Conditional Operator, also known as the "ternary operator." It has the syntax:
condition ? value-if-true : value-if-false
where:
In your example:
So, the operator works as follows:
This is equivalent to the following if statement:
if (max != 0) { hsb.s = 255 * delta / max; } else { hsb.s = 0; }
The above is the detailed content of How Does the JavaScript Conditional (Ternary) Operator Work?. For more information, please follow other related articles on the PHP Chinese website!