JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

What is the difference between && and || in JavaScript?

The difference between && and || is that the && operator returns true when both operands are true, whereas the || operator returns false when both operands are false.

The following truth table shows the difference between && and ||:

x y x && y x || y
true true true true
true false false true
false true false true
false false false false

Note: && is logical AND operator. || is logical OR operator.

Another difference between && and || is that the && operator has higher precedence than the || operator.

let x = true,
    y = false,
    z = true;

console.log(x || y && z); //true

First, y && z is evaluated, and it gave false. After that, x || false is computed, resulting in true. So, the overall value of the expression is true.

Recommended Posts