JavaScript ternary operator Usage Examples _ basics

Source: Internet
Author: User

Ternary operator Usage Example:

The ternary operator, as the first name indicates, requires three operands.

Grammar is a condition? Result 1: Results 2;. Here you write the terms on the question mark (?) followed by a colon (:) separated by a result of 1 and result 2. Satisfies the condition the result 1 otherwise the result 2.

Copy Code code as follows:

<script type= "Text/javascript" >
var b=5;
(b = = 5)? A= "true": A= "false";
document.write ("---------------------------" +a);
</script>

Result:---------------------------True
Copy Code code as follows:

<script type= "Text/javascript" >
var b=true;
(b = = False)? A= "true": A= "false";
document.write ("---------------------------" +a);
</script>

Result:---------------------------false

Introduction to ternary operators in programming languages

This operator is relatively rare because it has three operational objects. But it does belong to one of the operators because it eventually generates a value. This is different from the common If-else statement described in the latter section of this chapter. The expression takes the form of the following:

Copy Code code as follows:

Boolean expression? Value 0: value 1

If the result of the Boolean expression is true, the value 0 is computed, and its result becomes the value that is ultimately generated by the operator. However, if the result of a Boolean expression is False, the value 1 is computed, and its result becomes the value that is ultimately generated by the operator.

Of course, you can also swap common if-else statements (described later), but the ternary operators are more concise. Although C is proud of being a concise language, and the introduction of ternary operators is mostly to reflect this efficient programming, but if you intend to use it frequently, or to do some more thinking-it is easy to produce very readable code.

You can use the conditional operator for your own "side effects", or for the value it generates. However, you should usually use it for values, because doing so distinguishes the operator from the If-else explicitly. Here is an example:

Copy Code code as follows:

static int ternary (int i) {
Return I < 10? I * 100:i * 10;
}

As you can see, if you write the above code in a normal if-else structure, you'll have a lot more code than the above. As shown below:
Copy Code code as follows:

static int alternative (int i) {
if (I < 10)
return i * 100;
return I * 10;
}

But the second form is easier to understand and does not require more input. So when choosing ternary operators, make sure you weigh the pros and cons.

Related Article

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.