Home  >  Article  >  Web Front-end  >  JS ternary operation (ternary operation) method

JS ternary operation (ternary operation) method

高洛峰
高洛峰Original
2017-03-01 16:01:041748browse

1. What is ternary arithmetic: (Boolean expression? Value 0:Value 1;)

5>3?alert( 'Big 5'):alert('Big 3');

that is, if(5>3){alert('Big 5')}else{alert('Big 3')};

Note: The difference between ternary operation and if(){}else{} is that ternary operation has a return value

For example:

var max = a>b?a:b;

2. How to write multi-condition trinocular operation:

Example:Determine the four levels of ABCD based on student performance

var result = (sc<0 || sc>100) ?("Invalid score"):

sc>=90?("A"):

sc>=80?("B"):  

sc>=60?("C"):(" D");

Note: Priority issues need to be considered when calculating, add "()' to avoid incorrect results!

##3. 3. Advantages of ternary operations: concise and clear For reference, I also hope that everyone will support the PHP Chinese website For more articles related to JS ternary operations (ternary operations), please pay attention to the PHP Chinese website

Statement:
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