Home > Article > Web Front-end > Does javascript have conditional operators?
Javascript conditional operators. In js, the conditional operator is also called the "ternary operator", and the syntax is "conditional expression? expression 1 : expression 2;"; if the result of the "conditional expression" is true (true), then execute " The code in "Expression1", otherwise the code in "Expression2" is executed.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Conditional operator in javascript
In javascript, the conditional operator is also called the ternary operator, which is the only operator that uses ternary operators. An operator with one operand, consisting of a question mark and a colon. The syntax format is as follows:
条件表达式 ? 表达式1 : 表达式2 ;
If the result of "conditional expression" is true (true), then "expression 1" will be executed code in "Expression 2", otherwise the code in "Expression 2" will be executed.
Execution process
When the conditional operator is executed, the conditional expression is first evaluated
If the value is true, then execute expression 1
If the value is false, then execute expression 2
Example:
var a = 10; var b = 20; a > b ? alert("语句1"):alert("语句2")
[Related recommendations: javascript learning tutorial]
The above is the detailed content of Does javascript have conditional operators?. For more information, please follow other related articles on the PHP Chinese website!