As javascript is a loosely-typed language, we can perform logical operations on any type.
1 && 2;
null && undefined;
"hello" && true;
All the above operations are valid in javascript.
First, javascript decides whether an operand is truthy or falsy and, then it performs the logical operations on them.
A simple way to determine whether a value is truthy or falsy is by evaluating them in a boolean context.
if(value) { ... };
Boolean(value);
Syntax
Operand_1 && Operand_2 && Operand_3
Return Value
Starting from left, it returns the first operand that is false. If no operand evaluates to falsy value, it returns the last operand.
true && false && true;
// false
Let's consider the above example. The first falsy value we encounter is the second operand (false). Thus, the expression returns false.
{} && 0 && [];
// 0
{} and [] are truthy values in javascript. So, the above expression returns the second operand(0).