Javascript Operators

Logical AND (&&)

Description

Logical AND (&&) evaluates operands from left to right, returning immediately with the value of the first falsy operand it encounters; if all values are truthy, the value of the last operand is returned.

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:

false;
null;
NaN;
0;
-0;
0n;
empty string ("" or '' or ``);
undefined.

The AND operator preserves non-Boolean values and returns them as they are:

result = '' && 'foo';  // result is assigned "" (empty string)
result = 2 && 0;       // result is assigned 0
result = 'foo' && 4;   // result is assigned 4

Even though the && operator can be used with non-Boolean operands, it is still considered a boolean operator since its return value can always be converted to a boolean primitive. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator or the Boolean constructor.

Logical OR (||)

Syntax

expr1 || expr2

Description

If expr1 can be converted to true, returns expr1; else, returns expr2.

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.

Even though the || operator can be used with operands that are not Boolean values, it can still be considered a boolean operator since its return value can always be converted to a boolean primitive. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator or the Boolean constructor.

Last updated: 10/27/2022 2:33:17 PM

Latest Updates

© 0 - 2025 - Mike Brind.
All rights reserved.
Contact me at Mike dot Brind at Outlook.com