Logical Operators

 

摘要

 

运算符
实施于: JavaScript 1.0
ECMA 版本: ECMA-262

逻辑运算符通常用于布尔型(逻辑)值;这种情况,它们返回一个布尔型值。然而,&&和||运算符实际上返回一个指定操作数的值,因此这些运算符也用于非布尔型,它们返回一个非布尔型值。

 

The logical operators are described in the following table:

Operator Usage Description
Logical AND (&&) expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Logical OR (||) expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
Logical NOT (!) !expr Returns false if its single operand can be converted to true; otherwise, returns true.

Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (""), 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 values.

Short-Circuit Evaluation

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

  • false && anything is short-circuit evaluated to false.
  • true || anything is short-circuit evaluated to true.

The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.

For example, the following two functions are equivalent.

function shortCircuitEvaluation() {
  doSomething() || doSomethingElse()
}

function equivalentEvaluation() {
  var flag = doSomething();
  if (!flag) {
    doSomethingElse();
  }
}

Backward Compatibility

JavaScript 1.0 and 1.1

The && and || operators behave as follows:

Operator Usage Behavior
&& expr1 && expr2 If the first operand (expr1) can be converted to false, the && operator returns false rather than the value of expr1.
|| expr1 || expr2 If the first operand (expr1) can be converted to true, the || operator returns true rather than the value of expr1.

Examples

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

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

Logical NOT (!)

The following code shows examples of the ! (logical NOT) operator.

n1=!true              // !t returns false
n2=!false             // !f returns true
n3=!"Cat"             // !t returns false

 

Document Tags and Contributors

Contributors to this page: MoltBoy
最后编辑者: MoltBoy,