Home > Web Front-end > JS Tutorial > body text

Three different levels of operator &&_Basic knowledge

WBOY
Release: 2016-05-16 17:38:21
Original
1726 people have browsed it

The

operator can be understood at three different levels.

The first level of understanding

When the operands are both Boolean values, "&&" performs a Boolean AND operation on the two values.

Copy code The code is as follows:

x==0 && y==0 // only True is returned only when x and y are both 0

Relational operators have higher precedence than "&&".

The second level of understanding

"&&" can perform Boolean AND operation on true and false values. (False values ​​include false, null, undefined, 0, NaN and ""). Anywhere you want to use a boolean value in JS, expressions and statements will treat it as a true or false value, so in fact "&&" does not always return true and false.

Copy code The code is as follows:

null && true // =>null: left operation The number is false and returns it, the entire expression is false
true && (5 - 3) // =>2: The left operand is true, the right operand is calculated, and the result is returned

The third level of understanding

When an operator wants to return a true value or a false value, it will encounter two operation situations according to the value of the left operand: the operator first calculates the value of the left operand, if the calculation result is a false value, then the entire expression The result of the formula must also be a false value. At this time, "&&" simply returns the value of the left operand and does not calculate the right operand. If the left operand is true, "&&" evaluates the value of the right operand and returns it as the result of the entire expression.

Copy code The code is as follows:

var o = {x:1};
var p = null;
o && o.x; // =>1 o is a true value, return the value of o.x
p && p.y; // =>null: p is a false value, return it, Instead of calculating p.y

The behavior of "&&" is sometimes called "short-circuiting", and we will see that a lot of code takes advantage of this feature to conditionally execute code. For example, the following two lines of code are completely equivalent:

Copy code The code is as follows:

if(a == b) stop();
(a == b) && stop();// Equivalent to the above statement

Extension of knowledge

The operator "||", like "&&", also has some complex behaviors.

is used to select the first truth-valued expression from a set of alternative expressions:

Copy code The code is as follows:

// First check whether a is a true value, if so , return a, otherwise handle b in the same way as a
// If b is a true value, return b, otherwise return 5
var max = a || b || 5;

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

Copy code The code is as follows:

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

Operator precedence

For operators with the same priority, the order of operations is determined by the combination direction.

To simply remember it is:! > Arithmetic operators > Relational operators > && > || > Assignment operators

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template