How JavaScript Logical or operator (||) works

by Sai gowtham1min read

In JavaScript logical or(||) operator is used to evaluate the expressions from left to right.

It returns the first expression if it can be converted to true otherwise, it returns the second expression.

 expression 1 || expression 2 (short circuit evaluated)

The second expression is short circuit evaluated it means the second expression is executed or evaluated only if the first expression evaluates to false.

Examples:

console.log( 1 || 2 )  // 1

console.log( 0 || 10) // 10

console.log(false || true ) // true

console.log(false || false) // false

console.log( " " || "hello") // "hello"

console.log(" " || 0 ) // 0