A comprehensive understanding of javascript ternary operators

  • 2021-07-01 06:09:57
  • OfStack

3 yuan operator:

The 3-yuan operator represented by the name requires 3 operands.

Grammar is conditional? Result 1: Result 2; . Here you write the condition on the question mark (? ) is followed by result 1 and result 2 separated by a colon (:). Result 1 if the condition is met, otherwise result 2.


<script type="text/javascript"> 

var b=5; 

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

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

</script>

Outcome:-----true


<script type="text/javascript"> 

var b=true; 

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

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

</script>

Outcome:------false


Related articles: