[Go] js operator && | | and its priority

Source: Internet
Author: User

Tag: console var resolves als priority logic priority STR FALSE

First, && (logic and) operations, see a simple example:

var a = 1 && 2 && 3;
var b = 0 && 1 && 2;
var c = 1 && 0 && 2;
Console.log (a);//value is 3
Console.log (b);//value is 0
Console.log (c);//value is 0

The result of the operation is: 3,0,0.

Analytical:

The "&&" operation returns when it encounters false;

Calculation of variable A: Because 1 && 2,1 is true, returns 2;2&&3, 2 is true, and returns 3.

Calculation of variable B: Because 0 && 1,0 is False, returns 0, no longer calculates downward.

Second, | | (logical OR) operation, see a simple example:

var d = 0 | | 1 | | 2;
var e = 1 | | 0 | | 2;
var f = 1 | | 2 | | 0;

Console.log (d);//value is 1
Console.log (e);//value is 1
Console.log (f);//value is 1

Analytical:

"| |" The operation returns when it encounters true;

Calculation of the variable D: since 0 | | 1,0 is False, return 1;1 | | 2, 1 is true, returns 1.

Calculation of the variable e: since 1 | | 0,1 is true and returns 1, no longer counting down.

Iii. && (Logic and) and | | (logical OR) mixed use:

var g = 1 && 2 | | 3;

var h = 1 | | 2 && 3;

var i = 0 | | 2 && 3;

Console.log (g);//value is 2
Console.log (h);//value is 1

Console.log (h);//value is 3

Analytical:

&& (logical AND) priority is higher than | | (logical OR);

Calculation of the variable g: since 1 && 2,1 is true, return to 2;2 | | 3, 2 is True, returns 2.

Calculation of the variable e: Equation as 1 | | (a value), 1 is true, and finally returns 1 directly, without the need to calculate the value of the equation behind it.

Calculation of the variable I: Since 2 && 3,2 is true, return to 3;0 | | 3,0 is False and returns 3.

[Go] js operator && | | and its priority

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.