JS three-object operations (three-element operations) Method

Source: Internet
Author: User
The following small series will provide you with a detailed explanation of the JS three-object calculation (ternary calculation) method. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at it with xiaobian. 1. What is a three-object operation:(Boolean expression? Value 0: value 1 ;)

5> 3? Alert ('5 da'): alert ('3 da ');

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

Note: The three-object operation and the if () {} else {}The difference is that the three-object operation has a return value.

For example:

Var max = a> B? A: B;

2. How to Write the multi-condition three-object operation:

Instance:Determine four levels of ABCD based on student scores

Var result = (SC <0 | SC> 100 )? ("Invalid score "):

SC> = 90? (""):

SC> = 80? ("B "):

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

Note:You need to consider the priority during calculation. Add "()" to avoid result errors!

3. Advantages of the Three-object operation: concise and clear

The details of the above three-view JS operations (three-element operations) are all the content that I have shared with you. I hope to give you a reference and support for PHP.

For more articles on JS three-object operations (ternary operations), please refer to the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.