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