Home > Web Front-end > JS Tutorial > body text

ternary operator in javascript

怪我咯
Release: 2017-04-01 09:15:58
Original
1380 people have browsed it

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>
Copy after login

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

<script type="text/javascript"> 

var b=true; 

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

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

</script>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template