The content of this article is about what is the javascript ternary operator? The introduction to the usage of js ternary operator has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Speaking of js, it may be quite laborious for many beginners. I also feel that my js skills are not solid enough, so I will learn some things and share them in the hope that they can be helpful. People who need
"Ternary Operator" What is the ternary operator
Conditions? Execution if the condition is true: Execution if the condition is not true;
is equivalent to a simple if()else() statement
For example:
var num=10; if(num>5 && num<=10){ num++; }else{ num--; }
Ternary operator writing method
num>5 && num<=10 ? num++:num--; 三元运算符另外一种情况,相当于只有if()的语句,改写为三元运算符写法, var num=10; if(num>5 && num<=10){ num++; }
num>5 && num<=10?num :null; //null is used as a placeholder when the condition is not true symbol, if you don’t write anything after the colon, an error will be reported. You can use null, undefined, void 0 (that is, undefined) as a placeholder;
In a certain situation, use multiple statements to operate, use small Surrounded by brackets
##
var num=10; if(num>5 && num<=10){ num++; console.log(num); }
The above is the detailed content of What is the javascript ternary operator? Introduction to the usage of js ternary operator. For more information, please follow other related articles on the PHP Chinese website!