Logical operators are typically used for Boolean (logical) values, in which case they return a Boolean Value. however,&& and | | The operator actually returns a value of the specified operand, so these operators are also used for Non-boolean types, which return a Non-boolean Value.
DescribeEDIT
The following is a description of the logical operators:
operator |
Example |
Description |
Logic and ( && ) |
expr1&&expr2 |
Returns EXPR1 if Expr1 can be converted to false, otherwise returns expr2. therefore, when used in a Boolean environment, returns True if the two operation results are true, otherwise false is Returned. |
Logic or ( || ) |
expr1||expr2 |
Returns EXPR1 if the EXPR1 can be converted to true, otherwise expr2 is Returned. therefore, when used in a Boolean environment (in the conditional judgment of if), the results of both operations return TRUE if one is true, and false if both operations result in False. |
Logical Non ( ! ) |
!expr |
Returns False if a single expression can be converted to true, otherwise True. |
expressions that can be converted to false are: null,0, "", and Undefined.
Although &&
and ||
operators can be used in Non-boolean environments, they can also be considered Boolean if their return values can be converted to a Boolean Value.
Short Circuit calculation
Because the order of the logical expressions is left-to-right, you can also use the following rules for "short-circuit" calculations:
false && (anything)
The result of the short-circuit calculation is False.
true || (anything)
The result of the short-circuit calculation is True.
This rule ensures the accuracy of these calculations. Note that if the anything part of the above expression cannot be computed, neither side will take effect. it is also important to note that the anything part of the above expression is any single logical expression (in parentheses).
The two functions in the following sample code are Equal.
function shortCircuitEvaluation() { doSomething() || doSomethingElse()}function equivalentEvaluation() { var flag = doSomething(); if (!flag) { doSomethingElse(); }}
however, due to the existence of operator precedence, the following expressions do not have the same result. the right-hand side of the parentheses around becomes a stand-alone Expression.
false && true || true // 结果为 truefalse && (true || true) // 结果为 false
Logic and (&&)
The following code is an example of the && (logical and) Operator.
a1=true && true // t && t 结果为 truea2=true && false // t && f 结果为 falsea3=false && true // f && t 结果为 falsea4=false && (3 == 4) // f && f 结果为 falsea5="Cat" && "Dog" // t && t 结果为 Doga6=false && "Cat" // f && t 结果为 falsea7="Cat" && false // t && f 结果为 false
Logic or (| |)
The following code is | | An example of the (logical Or) Operator.
o1=true || true // t || t 结果为 trueo2=false || true // f || t 结果为 trueo3=true || false // t || f 结果为 trueo4=false || (3 == 4) // f || f 结果为 falseo5="Cat" || "Dog" // t || t 结果为 Cato6=false || "Cat" // f || t 结果为 Cato7="Cat" || false // t || f 结果为 Cat
Logical Non (!)
The following code is an !
example of the (logical non-) Operator.
n1=!true // !t 结果为 falsen2=!false // !f 结果为 truen3=!"Cat" // !t 结果为 false
Conversion rules convert and to OR
The following involves a mixed operation of the Boolean operation:
&& bCondition2
Equal to the Following:
!(!bCondition1 || !bCondition2)
Convert OR to and
The following involves a mixed operation of the Boolean operation:
|| bCondition2
Equal to the Following:
!(!bCondition1 && !bCondition2)
To delete nested parentheses
Because the logical expression is computed from left to right, you can usually remove the parentheses by following the rules BELOW.
Delete nested and
The following involves a mixed operation of the Boolean operation:
|| (bCondition2 && bCondition3)
Equal to the Following:
|| bCondition2 && bCondition3
To delete a nested OR
The following involves a mixed operation of the Boolean operation:
&& (bCondition2 || bCondition3)
Equal to the Following:
!(!bCondition1 || !bCondition2 && !bCondition3)
SpecificationEDIT
Specification |
Status |
Description |
ECMAScript 1st Edition. |
Standard |
Initial Definition. |
ECMAScript 5.1 (ECMA-262) Logical not Operator ECMAScript 5.1 (ECMA-262) Binary Logical Operators |
Standard |
|
ECMAScript (6th Edition, ECMA-262) Logical Not operator ECMAScript (6th Edition, ECMA-262) Binary Logical Operators |
Standard |
|
Browser compatibilityEDIT
Feature |
Chrome |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
Logic and ( && ) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
logical OR ( | | ) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Logical Non ( ! ) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Backwards compatibility: performance in JavaScript 1.0 and 1.1
&& | | The behavior of an expression is as Follows:
operator |
Example |
Description |
&& |
expr1 &&expr2 |
If the first expression (expr1) can be converted to false, then the && operation returns false instead of the expr1 Value. |
|| |
expr1 ||expr2 |
If the first expression (expr1) can be converted to true, then | | The operation returns true instead of the value of expr1. |
ResourcesEDIT
Document tags and contributors
contributors to this page: yenshen, teoli, moltboy
Last edited by: yenshen, Jan 5, 5:59:05 AM
JS | | &&