2016.7.29 Short circuit operation (logic operation is the most common one in short-circuit operation)

Source: Internet
Author: User

Written in front of the knowledge:

Boolean cast:

Except for Nan, null, "", Undefined, 0, function, which are false, the others are true;

About conversion, and so I'll write another one, dedicated to string, int, and Boolean conversions.

symbols for short-circuit operations: | |  &&! or with non-

the principle of short-circuit operation: when there are more than one expression, the value of the expression on the right is no longer evaluated when the left expression value can determine the result.

Rules for short-circuit operations:

&& find a fake, first look at the value of the first expression, if the first expression is not a Boolean type, cast to a Boolean type, if the conversion is false, the first value is returned (not the cast value), otherwise the second value is returned

|| For real, first look at the value of the first expression, if the first expression is not a Boolean type, cast to a Boolean type, and true after the conversion, returns the first value (not the value after being cast), otherwise returns the second value

! Non, look at the value of the expression, if the value of the expression is not a Boolean type, cast to a Boolean type, or True to return false, otherwise true.

Examples of short-circuit operations:

JavaScript code:

<script>Console.log ("& of short-circuit operations | Compares only the first expression and determines which value is returned by the first expression and the operation symbol"); Console.log ("&& Test"); Console.log ("&& for a holiday"); Console.log (true&&true); Console.log (true&&false); Console.log (false&&true); Console.log (false&&false); Console.log (true&& "ABS");//directly displays the value of the second expression absConsole.log (false&& "ABS");//directly displays the value of the first expression falseConsole.log ("" && "abs");//Nothing is displayed because the first expression is false after a Boolean cast, so the first value is displayedConsole.log ("abs" && "Qwe");//Direct Display QweConsole.log ("|| Test); Console.log ("|| Find the real "); Console.log (true||true); Console.log (true||false); Console.log (false||true); Console.log (false||false); Console.log (true||" ABS ");//Direct Display TrueConsole.log (false||" ABS ");//Direct Display ABSConsole.log ("| | |" ABS ");//Show ABSConsole.log ("abs" | | " Qwe ");//Show ABSConsole.log ("! Test"); Console.log (!true); Console.log (!false); Console.log (!" ABS ");//String ABS After Boolean cast is True,!true is falseConsole.log (! "");//string is false after a Boolean cast,!false is true</script>

Console:

                

2016.7.29 Short circuit operation (logic operation is the most common one in short-circuit operation)

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.