Main Menu

Categories

Subscribe

The Conditional Operator in JavaScript

April 6th, 2009

The conditional operator is the only JavaScript operator that takes three operands. The operator can have one of two values based on a condition. The syntax is:

condition ? val1 : val2

If condition is true, the operator has the value of val1. Otherwise it has the value of val2. You can use the conditional operator anywhere you would use a standard operator.

For example,

status = (age >= 18) ? “adult” : “minor”

No Comments

Leave a Reply