Three different levels of the operator && basic knowledge

Source: Internet
Author: User
Tags arithmetic operators

Operators can be understood from three different levels.

First Level understanding

When the operands are all Boolean, "&&" performs a Boolean and (and) operation on two values.

Copy Code code as follows:

X==0 && y==0//Only returns True if both X and Y are 0 o'clock

Relational operators have higher precedence than ' && '.

Second Level understanding

"&&" can perform Boolean and (and) operations on truth and false values. (The false value is false, null, undefined, 0, Nan, and ""). In any place in JS where you want to use a Boolean value, the expression and the statement treat it as a truth or a false value, so "&&" does not always return true and false.

Copy Code code as follows:

Null && True//=>null: The left operand is a false value and returns it, and the entire expression is false
True && (5-3)//=>2: The left operand is true, the right-hand operand is computed, and its result is returned

Third Level understanding

When an operator returns a true or False value, the two operations are encountered based on the value of the left operand: the operator first computes the value of the left-hand operand, and if the result of the calculation is false, the result of the entire expression must also be a false value, at which point "&&" simply returns the value of the left operand, The right-hand operand is not evaluated. If the left operand is true, "&&" calculates the value of the right-hand operand and returns it as the result of the entire expression.

Copy Code code as follows:

var o = {x:1};
var p = null;
o && o.x; =>1 O is the true value, and returns the values of o.x
P && p.y; =>null:p is a false value and returns it without calculating the P.Y

The "&&" behavior is sometimes referred to as "short-circuit", and we'll see a lot of code that uses this feature to execute code conditionally. For example, the following two lines of code are completely equivalent:

Copy Code code as follows:

if (a = = b) Stop ();
(A = = b) && stop ()//is equivalent to the above statement

Knowledge extension

Operator "| |" As well as "&&", there are some complex behaviors.

Used to select the first truth expression from a set of alternative expressions:

Copy Code code as follows:

First check whether a is true value, if it is, return a, otherwise and a treatment of the same way to deal with B
If B is the truth value returns B, otherwise returns 5
var max = a | | B | | 5;

This usage can be used in functions to provide default values for parameters:

Copy Code code as follows:

function Copy (o, p) {
p = P | | {}; If no object is passed to the parameter p, the newly created object is used
// ...
}

Operator Precedence

operator of the same priority, the order of operations is determined by the binding direction.

The simple note is:! > Arithmetic operators > Relational operators > && > | | > Assignment Operators

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.