Logical operators (JavaScript)
Operator | Description |
---|---|
expr1 && expr2 |
(Logical AND) Returns true if expr1 and expr2 are
both true. |
expr1 || expr2 |
(Logical OR) Returns true if either expr1 or expr2 is
true, or both are true. |
! expr1 |
(Logical NOT) Returns true if expr1 is
false; otherwise, returns false. |
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.
Usage
Best practice is to use logical operators on boolean operands. However, operands of any type can be combined. The strict rules are as follows:- For AND, the result is false if the first operand is false; otherwise, the result is the boolean value of the second operand.
- For OR, the result is true if the first operand is true; otherwise, the result is the boolean value of the second operand.
- For NOT, the result is true if the operand is false; otherwise, the result is true.
The following values are false when treated as boolean. Other
valid numeric and string values are treated as true.
- 0 or 0.0
- empty string
- null
- undefined
Examples
This example exercises the logical operators. It demonstrates how boolean and non-boolean operands are treated.function p(stuff) {
print("<<<" + stuff + ">>>");
}
var a = true;
var b = false;
var n0 = 0;
var n1 = 1;
var nm1 = -1;
var x0 = 0.0;
var x1 = 1.1;
var s0 = "";
var s1 = "foo";
var sn = null;
// How different types are treated as boolean
if(a) p("a is true"); else p("a is false"); // true
if(b) p("b is true"); else p("b is false"); // false
if(n0) p("n0 is true"); else p("n0 is false"); // false
if(n1) p("n1 is true"); else p("n1 is false"); // true
if(nm1) p("nm1 is true"); else p("nm1 is false"); // true
if(x0) p("x0 is true"); else p("x0 is false"); // false
if(x1) p("x1 is true"); else p("x1 is false"); // true
if(s0) p("s0 is true"); else p("s0 is false"); // false
if(s1) p("s1 is true"); else p("s1 is false"); // true
if(sn) p("sn is true"); else p("sn is false"); // false
// Using logical values as operands
p("a && a = " + (a && a)); // true
p("a && b = " + (a && b)); // false
p("b && b = " + (b && b)); // false
p("b && a = " + (b && a)); // false
p("a || a = " + (a || a)); // true
p("a || b = " + (a || b)); // true
p("b || b = " + (b || b)); // false
p("b || a = " + (b || a)); // true
p("!a = " + (!a)); // false
p("!b = " + (!b)); // true
// Using non-logical values as operands
p("n1 && n1 = " + (n1 && n1)); // true
p("n1 && n0 = " + (n1 && n0)); // false
p("n0 && n0 = " + (n0 && n0)); // false
p("n0 && n1 = " + (n0 && n1)); // false
p("n1 || n1 = " + (n1 || n1)); // true
p("n1 || n0 = " + (n1 || n0)); // true
p("n0 || n0 = " + (n0 || n0)); // false
p("n0 || n1 = " + (n0 || n1)); // true
p("!n1 = " + (!n1)); // false
p("!n0 = " + (!n0)); // true