Home >Web Front-end >JS Tutorial >Three different levels of operator &&_Basic knowledge
The
When the operands are both Boolean values, "&&" performs a Boolean AND operation on the two values.
Relational operators have higher precedence than "&&".
"&&" 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.
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.
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:
The operator "||", like "&&", also has some complex behaviors.
is used to select the first truth-valued expression from a set of alternative expressions:
This usage can be used in functions to provide default values for parameters:
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