Home > Web Front-end > JS Tutorial > What is the javascript ternary operator? Introduction to the usage of js ternary operator

What is the javascript ternary operator? Introduction to the usage of js ternary operator

不言
Release: 2018-09-19 17:42:54
Original
3395 people have browsed it

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

Ternary operator writing method

num>5 && num<=10 ? num++:num--;
三元运算符另外一种情况,相当于只有if()的语句,改写为三元运算符写法,
var num=10;
if(num>5 && num<=10){
 num++;
}
Copy after login

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);
}
Copy after login
The above example can be rewritten like this

num>5 && num<=10?(num, console.log(num)):null; Multiple statements are separated by commas

*Keywords such as break, continue, return, etc. cannot appear in the operation of the ternary operator;

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!

Related labels:
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