condition | operation
Executes one of two statements according to a condition.
test ?
Statement 1 :
statement2
Parameters
Test
Any Boolean expression.
Statement 1
The statement to execute when test is true . Can be a compound statement .
Statement 2
The statement to execute when test is false . Can be a compound statement.
Description
?: operator is a shortcut to a if...else statement. It is often used as part of a larger expression, and it is not consistent to use the if...else statement here. For example:
?: " day.");
In this example, if it is 6pm, a string containing the "good evening." is created. The equivalent code for using the if...else statement is as follows:
var now = new Date();var greeting = "Good";if (now.getHours() > 17)
greeting += " evening.";
else
greeting += " day.";
Requirements
Version 1
Please see
If...else Statement | Operator Precedence | Operator Summary