Javascript Boolean Operators








Boolean Operator NOT

The logical NOT operator is represented by an exclamation point ! and may be applied to any value in Javascript.

This operator always returns a Boolean value, regardless of the data type it's used on.

The logical NOT operator first converts the operand to a Boolean value and then negates it. The logical NOT behaves in the following ways:

  • If the operand is an object, false is returned.
  • If the operand is an empty string, true is returned.
  • If the operand is a nonempty string, false is returned.
  • If the operand is the number 0, true is returned.
  • If the operand is any number other than 0 (including Infinity), false is returned.
  • If the operand is null, true is returned.
  • If the operand is NaN, true is returned.
  • If the operand is undefined, true is returned.

console.log(!false);      //true
console.log(!"asdf");     //false
console.log(!0);          //true
console.log(!NaN);        //true
console.log(!"");         //true
console.log(!12);         //false

The code above generates the following result.





Boolean Operator NOT Example 2

The logical NOT operator can be used to convert a value into its Boolean equivalent.

By using two NOT operators, you can simulate the Boolean() casting function.


console.log(!!"asdf");     //true
console.log(!!0);          //false
console.log(!!NaN);        //false
console.log(!!"");         //false
console.log(!!12);         //true

The code above generates the following result.

Boolean Operator AND

The boolean operator AND operator is represented by the double ampersand << and is applied to two values:

var result = true && false;

Boolean operator AND behaves as described in the following truth table:

OPERAND 1OPERAND 2RESULT
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Boolean operator AND can be used with any type of operand, not just Boolean values.

When either operand is not a primitive Boolean, AND does not always return a Boolean value; instead, it does one of the following:

  • If the first operand is an object, the second operand is returned.
  • If the second operand is an object, the object is returned only if the first operand evaluates to true.
  • If both operands are objects, then the second operand is returned.
  • If either operand is null, then null is returned.
  • If either operand is NaN, then NaN is returned.
  • If either operand is undefined, then undefined is returned.

AND is a short-circuited operator: if the first operand determines the result, the second operand is not evaluated.

var found = true;
var result = (found && someUndeclaredVariable);    //error occurs here
console.log(result);    //this line never executes

found = false;
var result = (found && someUndeclaredVariable);    //no error
console.log(result);    //works





Boolean Operator OR

The Boolean Operator OR is represented by the double pipe || in Javascript, like this:

var result = true || false;

OR behaves as described in the following truth table:

OPERAND 1OPERAND 2RESULT
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

If either operand is not a Boolean, logical OR will not always return a Boolean value; instead, it does one of the following:

  • If the first operand is an object, then the first operand is returned.
  • If the first operand evaluates to false, then the second operand is returned.
  • If both operands are objects, then the first operand is returned.
  • If both operands are null, then null is returned.
  • If both operands are NaN, then NaN is returned.
  • If both operands are undefined, then undefined is returned.

OR operator is short-circuited. In this case, if the first operand evaluates to true, the second operand is not evaluated.

var found = true;
var result = (found || someUndeclaredVariable);    //no error
console.log(result);    //works

found = false;
var result = (found || someUndeclaredVariable);    //error occurs here
console.log(result);    //this line never executes

Because the variable found is set to true, the variable someUndefinedVariable is never evaluated and thus the output is "true". If the value of found is changed to false, an error will occur.

To avoid assigning a null or undefined value to a variable, use the following code.

var myObject = preferredObject || backupObject;

If preferredObject isn't null, then it's assigned to myObject; if it is null, then backupObject is assigned to myObject.