Detailed Explanation of JS Trinocular Operation of Ternary Operation Method

  • 2021-07-24 09:54:51
  • OfStack

1. What is a 3-order operation: (Boolean expression? Value 0: Value 1; )

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

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

Note: The difference between 3-purpose operations and if () {} else {} is that 3-purpose operations have return values

For example:

var max = a > b?a:b;

2. How to write the 3-purpose operation with multiple conditions:

Example: ABCD 4 grades are judged according to students' grades

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

sc > =90?("A"):

sc > =80?("B"):

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

Note: Priority should be considered when calculating, and "()" should be added to avoid errors in results!

Advantages of 3, 3-purpose operation: conciseness and clarity


Related articles: