Menu

JavaScript Logical Operators

Operator Operator type A About Value types
! … Logical NOT (inversion) from right to left 1 any → boolean
… && … Logical AND from left to right 2 any, any → any
… || … Logical OR from left to right 2 any, any → any

The ! (logical NOT) operator converts (if necessary) the value of its operand to a boolean value, then inverts that value and returns the resulting boolean value:

var a = true, b = false;
alert(!a); // false
alert(!b); // true

If the ! operator is applied twice to an operand, the value will be converted to its logical equivalent:

The && operator (logical AND) evaluates the value of the left operand first. If the left operand is false or a value that can be converted to false, then the value of the left operand is returned, but the value of the right operand is not evaluated:

vary = 1;
var x = 0 && ++y; // ++y will not be evaluated

alert("x: " + x); // 0
alert("y: " + y); // one

If the left operand is true or a value that can be converted to true, the operator proceeds to evaluate the value of the right operand, and returns the value of the right operand:

var x = 1 && 2;
var y = 1 && 0;

alert("x: " + x); // 2
alert("y: " + y); // 0

The || operator (logical OR) evaluates the value of the left operand first. If the left operand is true or a value that can be converted to true, then the value of the left operand is returned, but the value of the right operand is not evaluated:

vary = 1;
var x = 1 || ++y; // ++y will not be evaluated

alert("x: " + x); // one
alert("y: " + y); // one

If the left operand is false or a value that can be converted to false, the operator proceeds to evaluate the value of the right operand, and returns the value of the right operand:

var x = 0 || 2;
var y = null || 0;

alert("x: " + x); // 2
alert("y: " + y); // 0

Follow us on social networks

Feedback