Summary of JavaScript logical operators
- 2021-08-12 01:48:01
- OfStack
Preface
With regard to the logical operators in JavaScript, we often use them but may not know some of their mechanisms and usage.
Mechanism
First of all, we need to know that the priorities of several logical operators are different (for the complete operator priority, see operator priority), and logic is not > Logical and > Logical or > Conditional operator (3-item operator). Operational order conditional operators are right-to-left, while logical AND and logical OR are both left-to-right.
// The precedence of the expression causes different results
false && true || true // The result is true
false && (true || true) // The result is false
The logical operation expression returns the value of the word expression instead of 1
Boolean
It's just that many times where we use logical expressions helps us cast, such as
if
Statements, etc.
Logical operators are usually used for Boolean (logical) values. In this case, they return 1 Boolean value. However, & & The and operators return the value of 1 specified operand, so these operators are also used for non-Boolean values. At this time, they will also return a non-Boolean value.
Logical and
expression1 && expression2
The mechanism of is that if
expression1
Can be converted to
true
Then return
expression2
Otherwise, return
expression1
.
Logical or
expression1 || expression2
The mechanism of is that if
expression1
Can be transformed into
true
Then return
expression1
Otherwise, return
expression2
.
Logical non-
!expression
If
expression
Can be transformed into
true
Returns the
false
Otherwise, return
true
.
expression may be any 1 type, but not 1 is a Boolean value.
Will be converted to
false
The expressions of are:
What needs special attention is that
undefined
Some expressions return theundefined
For example, there is no settingreturn
The return value of the function execution of isundefined
.
Although
&&
And
||
Operators can use non-Boolean operands, but they can still be considered Boolean operators because their return values can always be converted to Boolean values. If you want to explicitly convert their return values (or expressions) to Boolean values, use double non-operators (that is,
!!
) Or
Boolean
Constructor.
Double non-operator
!!
You can cast any value to a Boolean value, which is often used where conditional judgment is needed.
Short circuit calculation
The mechanism of logic operation also exists short circuit calculation:
(some falsy expression1) && (expression2)
The result of short circuit calculation is false.
(some truthy expression1) || (expression2)
The result of short circuit calculation is true.
Short circuit means that in the above expression
expression2
Part will not be executed, so
expression2
Any side effects of will not take effect (for example, if
expression
Is a function call, this call will not occur). The reason for this phenomenon is that the value of the whole expression has been determined after the first operand has been evaluated.
Usage
Utilization
expression1
2
Logical operators support arbitrary types and short-circuit calculation. We can apply logical operators to some special places.
Logical and
Logical and can be used to get the first false value, such as
expr1 && expr2 && expr3
Is returned when there is a false value in it. It can also be analogized to execute the last expression when the previous expressions are all true to simplify the code of judgment logic, such as
x > 0 && a()
This can be used instead of
if
Statement.
Logical or
Logic may be used to set default values. For example, your function requires the user to enter 1 parameter, and if the user does not enter 1 default value.
this.a = param || {}
.
The above is the JavaScript logical operator related summary of the details, more information about the JavaScript logical operator please pay attention to other related articles on this site!