邏輯運算子通常會搭配 Boolean
(logical) 值。若是,則回傳布林值。然而, &&
和 ||
運算子通常回傳其中一運算元的值, 因此若這些運算子搭配非布林值使用,他們會回傳非布林值。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
說明
邏輯運算子的使用方式如下(expr
可能會是 type,不只是布林值):
Operator | Syntax | Description |
---|---|---|
邏輯 AND (&& ) |
expr1 && expr2 |
若 expr1 可被轉換成 true , 回傳 expr2 ; 否則 回傳 expr1 . |
邏輯 OR (|| ) |
expr1 || expr2 |
若 expr1 可被轉換成 true , 回傳 expr1 ; 否則 回傳 expr2 . |
邏輯 NOT (! ) |
!expr |
回傳 false 若它的單一運算元可被轉換成 true ; 否則回傳 true . |
If a value can be converted to true
, the value is so-called truthy. If a value can be converted to false
, the value is so-called falsy.
Examples of expressions that can be converted to false are:
null
;NaN
;0
;- empty string (
""
or''
or``
); undefined
.
Even though the &&
and ||
operators can be used with operands that are not Boolean values, they can still be considered boolean operators since their return values can always be converted to boolean primitives. To explicitly convert their return value (or any expression in general) to the corresponding boolean value, use a double NOT operator or the Boolean constructor.
Short-circuit evaluation
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
(some falsy expression) && expr
is short-circuit evaluated to the falsy expression;(some truthy expression) || expr
is short-circuit evaluated to the truthy expression.
Short circuit means that the expr parts above are not evaluated, hence any side effects of doing so do not take effect (e.g., if expr is a function call, the calling never takes place). This happens because the value of the operator is already determined after the evaluation of the first operand. See example:
function A(){ console.log('called A'); return false; }
function B(){ console.log('called B'); return true; }
console.log( A() && B() );
// logs "called A" due to the function call,
// then logs false (which is the resulting value of the operator)
console.log( B() || A() );
// logs "called B" due to the function call,
// then logs true (which is the resulting value of the operator)
Operator precedence
The following expressions might seem equivalent, but they are not, because the &&
operator is executed before the ||
operator (see operator precedence).
true || false && false // returns true, because && is executed first
(true || false) && false // returns false, because operator precedence cannot apply
Logical AND (&&
)
The following code shows examples of the &&
(logical AND) operator.
a1 = true && true // t && t returns true
a2 = true && false // t && f returns false
a3 = false && true // f && t returns false
a4 = false && (3 == 4) // f && f returns false
a5 = 'Cat' && 'Dog' // t && t returns "Dog"
a6 = false && 'Cat' // f && t returns false
a7 = 'Cat' && false // t && f returns false
a8 = '' && false // f && f returns ""
a9 = false && '' // f && f returns false
Logical OR (||
)
The following code shows examples of the ||
(logical OR) operator.
o1 = true || true // t || t returns true
o2 = false || true // f || t returns true
o3 = true || false // t || f returns true
o4 = false || (3 == 4) // f || f returns false
o5 = 'Cat' || 'Dog' // t || t returns "Cat"
o6 = false || 'Cat' // f || t returns "Cat"
o7 = 'Cat' || false // t || f returns "Cat"
o8 = '' || false // f || f returns false
o9 = false || '' // f || f returns ""
o10 = false || varObject // f || object returns varObject
Logical NOT (!
)
The following code shows examples of the !
(logical NOT) operator.
n1 = !true // !t returns false
n2 = !false // !f returns true
n3 = !'' // !f returns true
n4 = !'Cat' // !t returns false
Double NOT (!!
)
It is possible to use a couple of NOT operators in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value (see truthy and falsy).
The same conversion can be done through the Boolean
function.
n1 = !!true // !!truthy returns true
n2 = !!{} // !!truthy returns true: any object is truthy...
n3 = !!(new Boolean(false)) // ...even Boolean objects with a false .valueOf()!
n4 = !!false // !!falsy returns false
n5 = !!"" // !!falsy returns false
n6 = !!Boolean(false) // !!falsy returns false
Conversion rules for booleans
Converting AND to OR
The following operation involving booleans:
bCondition1 && bCondition2
is always equal to:
!(!bCondition1 || !bCondition2)
Converting OR to AND
The following operation involving booleans:
bCondition1 || bCondition2
is always equal to:
!(!bCondition1 && !bCondition2)
Converting between NOTs
The following operation involving booleans:
!!bCondition
is always equal to:
bCondition
Removing nested parentheses
As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.
Removing nested AND
The following composite operation involving booleans:
bCondition1 || (bCondition2 && bCondition3)
is always equal to:
bCondition1 || bCondition2 && bCondition3
Removing nested OR
The following composite operation involving booleans:
bCondition1 && (bCondition2 || bCondition3)
is always equal to:
!(!bCondition1 || !bCondition2 && !bCondition3)
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) | Standard | Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators |
ECMAScript 2015 (6th Edition, ECMA-262) | Standard | Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators |
ECMAScript (ECMA-262) | Living Standard | Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators |
Browser compatibility
No compatibility data found. Please contribute data for "javascript.operators.logical" (depth: 1) to the MDN compatibility data repository.