var b=5; (b == 5) ? a="true" : a="false";"/> var b=5; (b == 5) ? a="true" : a="false";">

Home  >  Article  >  Web Front-end  >  ternary operator in javascript

ternary operator in javascript

怪我咯
怪我咯Original
2017-04-01 09:15:581366browse

Ternary operator operator:

The ternary operator as the name indicates requires three operations number.

The syntax is condition ? result 1 : result 2;. Here you write the condition in front of the question mark (?) followed by result 1 and result 2 separated by a colon (:). If the condition is met, the result is 1, otherwise the result is 2.

<script type="text/javascript"> 

var b=5; 

(b == 5) ? a="true" : a="false"; 

document.write(" --------------------------- "+a); 

</script>

Result: -------------------------- true

<script type="text/javascript"> 

var b=true; 

(b == false) ? a="true" : a="false"; 

document.write(" --------------------------- "+a); 

</script>

Result: --------------------------- false

The above is the detailed content of ternary operator in javascript. 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