Truthy Values in JavaScript

Nov 4, 2019

In JavaScript, a value is truthy if JavaScript's built-in type coercion converts it to true. Every value is either truthy or falsy, so any value that isn't falsy must be truthy.

Truthy and falsy usually come up in the context of if statements. For example, the below if statement will print if and only if v is truthy.

if (v) {
  console.log('v is truthy!');
}

Remember that there are only 7 values in JavaScript that are falsy:

Every other value is truthy. For example, even a Boolean object containing false is truthy.

const v = new Boolean(false);

// Will print! All JavaScript objects are truthy.
if (v) {
  console.log('v is truthy!');
}

With the Logical NOT Operator

The logical NOT operator in JavaScript converts truthy values to false and falsy values to true. In other words, the logical ! operator lets you store the value JavaScript's built-in boolean coercion converts v into.

// `b` will be false if `v` is truthy
const b = !v;

You might see the logical NOT operator twice in a row: !!v. This is how you convert a value to a boolean using JavaScript's built-in coercion.

// `isTruthy` is true if and only if `v` is truthy.
const isTruthy = !!v;

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!