Logical Operators in JavaScript

Oct 30, 2019

JavaScript has 3 logical operators:

Logical NOT

The logical not operator ! checks whether a value is truthy or falsy. In other words, !v === false unless v is one of the below 7 values:

// Falsy values means `!v === true`
!false; // true
!0; // true
!''; // true
!undefined; // true
!null; // true
!Number('abc'); // true

// All other values are "truthy", meaning `!v === false`
!1; // false
!({}); // false
!({ valueOf: () => 0 }); // false
!' '; // false

The not operator always returns a boolean, unlike || and &&. A common way to convert a value v to a boolean is to use the logical not operator twice:

// Convert `v` to a boolean
!!v;

Logical OR

The logical or operator || operates on 2 values, and returns a truthy value if at least one of the values is truthy.

// True if `a` or `b` is truthy. Will only be `false` if
// both `a` and `b` are falsy.
!!(a || b);

The logical or operator is most often used in if statements to execute the if block if one of multiple conditions is true. But, the logical or operator does not necessarily return a boolean:

This works neatly with multiple or operators: a || b || c || d returns the leftmost value that is truthy. Because of this, logical or is often used for default values:

const defaultNumReps = 2;

function repeat(str, numReps) {
  // If `numReps` is null, undefined, or 0, this sets
  // `numReps` to the default value `2`
  numReps = numReps || defaultNumReps;

  let ret = '';
  for (let i = 0; i < numReps; ++i) {
    ret += str;
  }
  return ret;
}

Logical AND

The logical and operator && operates on 2 values, and returns falsy if at least one of the values is falsy:

// Only true if both `a` and `b` are truthy.
!!(a && b);

The logical and operator is most often used in if statements to execute the if block if multiple conditions are all true. But, like the logical or operator, the logical and operator doesn't necessarily return a boolean:

Given a chain of multiple logical and operators a && b && c && d, JavaScript returns the left-most falsy value.


Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!