An explanation of the ternary operator usage:
This operator is a very common element symbol and can save a lot of code if used properly. The ternary operator can also be called as a conditional operator, which can be said to be a simplified form of the If...else statement, the following is a simple example of how to implement the sub-operator, first look at the syntax structure of the operator:
Expression one? Expression two: expression three
A ternary operator is called ternary because it has three operands. When the return value of expression one is true, the value of expression two is returned, otherwise the value of expression three is returned. Let's look at a code example:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"/><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ternary operator usage details-ant tribe</title><styletype= "Text/css">Div{width:200px;Height:50px;margin:0px Auto;Background-color:Green;text-align:Center;Line-height:50px;Color:Red;}</style><Scripttype= "Text/javascript">window.onload=function(){ vara=3, b=4 varm; M=a>b?"a greater than B":"A is less than B"; document.getElementById ("Num"). InnerHTML=m;}</Script></Head><Body><DivID= "num"></Div></Body></HTML>
In the above code, the function of the ternary operator is to determine whether a is greater than B, if greater than the return "a greater than B" otherwise return "a less than B".
The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0428/306.html
An explanation of ternary operator usage