JavaScript short-circuit expression

Source: Internet
Author: User

Short-circuit expression: as "&&" and "| |" Operator's operand expression, which is evaluated as long as the final result can be determined to be true or FALSE, the evaluation process terminates, which is called a short-circuit evaluation. This is an important property of these two operators.

Code:

1         // || (If a has a value then take a, and vice versa, take B)2         varFoo = a | |b;3         //equivalent4         if(a) {5Foo =A;6}Else {7Foo =b;8         }9 Ten         //&& (If a has a value then take B and vice versa) One         varBar = a &&b; A         //equivalent -         if(a) { -Bar =b; the}Else { -Bar =A; -}

Using this kind of short-circuit expression can make the code look concise and improve the coding efficiency.

JavaScript short-circuit expression

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.