Examples of javascript ternary operators and javascript Operators

Source: Internet
Author: User

Examples of javascript ternary operators and javascript Operators

Example of ternary operators:

For example, the name represents three operands.

Is the syntax a condition? Result 1: result 2. Here you write the condition in question mark (?) Result 1 and result 2 are separated by colons. If the conditions are met, result 1 is returned. Otherwise, result 2 is returned.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var B = 5;
(B = 5 )? A = "true": a = "false ";
Document. write ("---------------------------" + );
</Script>

Result: --------------------------- true
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var B = true;
(B = false )? A = "true": a = "false ";
Document. write ("---------------------------" + );
</Script>

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

Introduction to ternary operators in programming languages

This operator is rare because it has three operation objects. But it does belong to one of the operators, because it will eventually generate a value. This is different from the general if-else statement described in the later section of this chapter. Expressions take the following form:
Copy codeThe Code is as follows:
Boolean expression? Value 0: value 1

If the result of a Boolean expression is true, the "value 0" is calculated, and the result is the final value produced by the operator. However, if the result of a Boolean expression is false, the value is 1, and the result is eventually produced by the operator.

Of course, you can also use common if-else statements (described later), but the ternary operators are more concise. Although C is proud that it is a concise language, and the introduction of ternary operators is mostly to reflect this efficient programming, if you plan to use it frequently, you still need to think more first-it can easily produce code with poor readability.

Conditional operators can be used for their own "Side effects" or values generated by them. However, it should usually be used for values, because the operator and if-else can be clearly differentiated. The following is an example:
Copy codeThe Code is as follows:
Static int ternary (int I ){
Return I <10? I * 100: I * 10;
}

It can be seen that if the above Code is written in a normal if-else structure, the amount of code will be much larger than the above. As follows:
Copy codeThe Code is as follows:
Static int alternative (int I ){
If (I <10)
Return I * 100;
Return I * 10;
}

However, the second form is easier to understand and does not require more input. Therefore, when selecting a ternary operator, you must weigh the advantages and disadvantages.

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.