--- title: Logical operators slug: Web/JavaScript/Reference/Operators/Logical_Operators translation_of: Web/JavaScript/Reference/Operators translation_of_original: Web/JavaScript/Reference/Operators/Logical_Operators ---
邏輯運算子通常會搭配 {{jsxref("Boolean")}} (logical) 值。若是,則回傳布林值。然而, && 和 || 運算子通常回傳其中一運算元的值, 因此若這些運算子搭配非布林值使用,他們會回傳非布林值。
邏輯運算子的使用方式如下(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 {{Glossary("truthy")}}. If a value can be converted to false, the value is so-called {{Glossary("falsy")}}.
Examples of expressions that can be converted to false are:
null;NaN;0;"" 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.
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)
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
&&)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
||)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
!)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
!!)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 {{Glossary("truthy")}} and {{Glossary("falsy")}}).
The same conversion can be done through the {{jsxref("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
The following operation involving booleans:
bCondition1 && bCondition2
is always equal to:
!(!bCondition1 || !bCondition2)
The following operation involving booleans:
bCondition1 || bCondition2
is always equal to:
!(!bCondition1 && !bCondition2)
The following operation involving booleans:
!!bCondition
is always equal to:
bCondition
As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.
The following composite operation involving booleans:
bCondition1 || (bCondition2 && bCondition3)
is always equal to:
bCondition1 || bCondition2 && bCondition3
The following composite operation involving booleans:
bCondition1 && (bCondition2 || bCondition3)
is always equal to:
!(!bCondition1 || !bCondition2 && !bCondition3)
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. |
| {{SpecName('ES5.1', '#sec-11.11')}} | {{Spec2('ES5.1')}} | Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators |
| {{SpecName('ES6', '#sec-binary-logical-operators')}} | {{Spec2('ES6')}} | Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators |
| {{SpecName('ESDraft', '#sec-binary-logical-operators')}} | {{Spec2('ESDraft')}} | Defined in several sections of the specification: Logical NOT Operator, Binary Logical Operators |
{{Compat("javascript.operators.logical")}}