Falsy and Truthy in JavaScript

Falsy and Truthy in JavaScript

LinkFalsy

Falsy values are values that are considered false when encountered in a boolean context. That means values that become false if you try to convert them to a boolean.

TypeScript
Boolean('');
//=> false

Boolean(0);
//=> false

Boolean(null);
//=> false

The list grows over time, but currently, those are the falsy values in JavaScript:

  1. false
  2. 0 -0 0n representations of zero
  3. ```` "" '' empty string
  4. null
  5. undefined
  6. NaN not a number
  7. document.all possibly
markdown
**document.all**
Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.
That slot only exists in document.all and cannot be set using JavaScript.

LinkTruthy

Truthy values are the opposite. They are values that are considered true when encountered in a boolean context.

TypeScript
Boolean('abc');
//=> true

Boolean(1);
//=> true

Boolean([]);
//=> true

All values that are not falsy, are truthy.

LinkConclusion

References are in the references.

We release web development tutorials every two weeks. Consider subscribing if you're interested in that.

Have a great day, and I'll see you soon!

LinkReferences

  1. Truthy definition on MDN
  2. Falsy definition on MDN
  3. document.all edge case on MDN

Join the newsletter and be the first to know when I launch a course, post a video or write an article.

I don't SPAM, one email per week tops.