Home  >  Article  >  Web Front-end  >  Does javascript have conditional operators?

Does javascript have conditional operators?

青灯夜游
青灯夜游Original
2022-02-16 18:19:512020browse

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.

Does javascript have conditional operators?

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")

Does javascript have conditional operators?

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn