11 Jan
11Jan


Logical OR (||) operator

This operator for a set of operands is true if and only if one or more of its operands is true

const num1 = 10, num2 = 20;

true || false;                // true
false || false;               // false
false || num1;                // 10
0 || num2;                    // 20
"text" || true                // "text"
num1 > 0 || num2 < 0          // true


Logical AND (&&) operator

This operator for a set of operands is true if and only if all of its operands are true

const num1 = 10, num2 = 20;

true && true;                 // true
true && false;                // false
true && num1;                 // 10
num1 && num2;                 // 20
"text" && (num1 + num2)       // 30
num1 > 0 && num2 < 0          // false


Nullish coalescing (??) operator

This is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand

undefined ?? 10;              // 10
null ?? 20;                   // 20
false ?? num1;                // false
0 ?? num2;                    // 0


Double NOT (!!) operator

This operator used to explicitly force the conversion of any value to the corresponding boolean primitive.

!!10;                         // true
!!{};                         // true
!!"";                         // false
!!0;                          // false



Notes

It is not possible to combine both the AND (&&) and OR operators (||) directly with ??



Comments
* The email will not be published on the website.