Logical operators in JavaScript
JavaScript has three logical operators:
||
which represents logical "OR"&&
which represents logical "AND"!
which represents logical "NOT".
||
(OR)
The "OR" logical operator returns true
if either operand1 OR operand2 is true
.
Syntax:
operant1 || operant2
For example:
alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false
From the table above we can construct the truth table of the "OR" logic operator.
operant1 | operant2 | result |
---|---|---|
true | true | true |
false | true | true |
true | false | true |
false | false | false |
Generally, the logical "OR" is supposed to deal with boolean operands only. However, in JavaScript, any operand that is not boolean is first converted to boolean before the evaluation.
This means that a number like 1
is treated as true
and a number like 0
as false
. For example:
console.log( 1 || 1 ); // 1
console.log( 0 || 1 ); // 1
console.log( 1 || 0 ); // 1
console.log( 0 || 0 ); // 0
In actual fact, it is only 0 that is treated as false
, all the other numbers are treated as true
.
console.log( 2 || 2 ); // 2
console.log( 0 || 2 ); // 2
console.log( 2 || 0 ); // 2
console.log( 0 || 0 ); // 0
The logical "OR" can be used to test if any one of the given conditions of an if
statement is true
. For example:
let hourOfDay = 5;
let lightIntensity = 0;
if (hourOfDay > 6 || lightIntensity < 0.5) {
console.log( 'Switch on the Lights' ); // Switch on the Lights
}
The example above is a simulation of an automatic light switch using JavaScript. If the hour of the day is after 6 or if the light intensity is less than 50% it will log a message ("Swicth on the lights") to console. Only one of the conditions has to be true.
We can have as many conditions as we want:
value1 || value2 || value3 || value4
No matter how many values we have, the "OR" logical operator requires at least only one of the operands to be true
and here is how it works:
- the operands are evaluated from left to right.
- if any operand is
true
, the logic stops there and returns the value of that operand. - If all operands are
false
the last operand is returned.
// returns the fist true value
console.log( 0 || 2 || 1 ); // 2
console.log( 0 || 1 || 2 ); // 1
// if no operant is true, it returns the last false value
console.log( null || undefined || 0 ); // 0
console.log( 0 || null || undefined ); // undefined
console.log( 0 || undefined || null ); // null
A value is returned in the original form of the operand, without the conversion.
Every non-empty string is considered true
. This means that while 0
is false, "0"
is true
because its a non-empty string.
// numerical 0 is false and string 0 is true
console.log(Boolean(0) ); // false
console.log(Boolean("0")); // true
// returns the first true value in its original form
console.log( 0 || 1 ); // 1
console.log( "0" || 1 ); // 0
&&
(AND)
The "AND" logical operator returns true
if operand1 AND operand2 are both true
.
Syntax
operand1 && operand2;
In classical programming AND returns true
if both operands are truthy and false
otherwise:
alert( true && true ); // true
alert( false && true ); // false
alert( true && false ); // false
alert( false && false ); // false
Its truth table:
operant1 | operant2 | result |
---|---|---|
true | true | true |
false | true | false |
true | false | false |
false | false | false |
For numerical values 1 is true
and 0 is false
.
console.log( 1 && 1 ); // 1
console.log( 0 && 1 ); // 0
console.log( 1 && 0 ); // 0
console.log( 0 && 0 ); // 0
No matter how many values we have, the "AND" logical operator requires all of the operands to be true
and here is how it works:
- the operands are evaluated from left to right.
- if all operands are
true
, the logic returns the value of the last operand. - If any of the operands is
false
the firstfalse
operand is returned.
// returns the last true value
console.log( 1 && 2 && 3 ); // 3
console.log( 2 && 3 && 2 ); // 2
// if any operand is false, it returns the first false value
console.log( null && undefined && 0 ); // null
console.log( 0 && null && undefined ); // 0
console.log( 0 && undefined && null ); // 0
console.log( 1 && undefined && 0 ); // undefined
console.log( 1 && null && undefined ); // null
console.log( 1 && undefined && null ); // undefined
!
(NOT)
The "NOT" logical operator returns true
if the operand is not true
.
Syntax:
!operand
Example:
// returns the inverse of the boolean value of the operand
console.log( !true ); // false
console.log( !0 ); // true
console.log( !1 ); // false
console.log( !"0" ); // false
console.log( !false ); // true
console.log( !23 ); // false