Skip to content
Theory · Operators and type conversion · lesson 8 of 14

Truthy and falsy values

The &&, || and ! operators check a value as if it were a boolean. It is like asking a box "is there anything in it?": an empty box answers "no", and a box with things in it answers "yes". For the number 0 the answer is "false", but for the string "0" it is already "true":

JavaScript
console.log(Boolean(0), Boolean("0")); // false true
console.log(Boolean(""), Boolean(" ")); // false true
console.log(Boolean(null), Boolean("false")); // false true

Let's walk through the program line by line:

  1. The number 0 is falsy, while the string "0" is truthy: it holds a character.
  2. The empty string is falsy, while a string of one space is truthy.
  3. null is falsy, while the string "false" is truthy, even though it says "false".

The Boolean(x) function turns a value into true or false by the same rules the logical operators use. Values it turns into false are called falsy, and the rest are truthy. For strings only the length matters: the empty string is falsy, and any other string is truthy, even a string of one space or the word "false".

Eight falsy values

You already saw all eight falsy values in the lesson on logical operators:

  • false;
  • 0, -0 and 0n;
  • "" — the empty string;
  • null and undefined;
  • NaN.

Minus zero is almost indistinguishable from ordinary zero: the comparison -0 === 0 gives true. This list is worth learning: interviewers often ask you to name it.

Everything else is truthy

Any value not on the list is truthy. That includes the empty array [], a list of values with no elements in it: it is truthy too.

Here the box picture stops working. The array [] is empty, like a box with nothing in it, but to the question "is there anything in it?" JavaScript answers "yes". What matters to it is that the array itself exists, not what it holds.

NaN and a fallback value

Since NaN is falsy, the result of a failed conversion can be replaced with a fallback value: Number("abc") || 0 gives 0, because || skips the falsy NaN. The ?? operator will not do that: it looks only at null and undefined, and NaN is neither.

This replacement has a flaw you know from the lesson on logical operators. Number("0") || 7 gives 7: a real zero is falsy too, and || replaced it along with NaN.

Double negation

The ! operator turns a value into a boolean and flips it. Two ! in a row flip twice, so !!x gives the same as Boolean(x):

JavaScript
console.log(!!"Ann", !!42, !![]); // true true true
console.log(!!NaN, !!-0, !!0n); // false false false
console.log(!"", !!""); // true false

Line by line:

  1. !!"Ann", !!42 and !![] give true: all three values are truthy.
  2. !!NaN, !!-0 and !!0n give false: all three are on the falsy list.
  3. !"" gives true, and !!"" gives false: the first ! flips the answer, and the second flips it back.

!!x is shorter, and Boolean(x) is clearer to read. Their result is always the same, and it is always true or false, not the value x itself. This turns any value into a "yes or no" answer. For example, !!name tells whether the string name has at least one character.

Common mistake

A "no" answer is kept in a string and turned into a boolean:

JavaScript
const answer = "false";
console.log(Boolean(answer)); // ✗

The program prints true: the string "false" is not empty, so it is truthy. Compare text with text: answer === "true" gives false.

Key points

  • There are eight falsy values: false, 0, -0, 0n, "", null, undefined and NaN.
  • All other values are truthy, even "0", "false" and the empty array [].
  • Boolean(x) and !!x turn any value into true or false.
  • || replaces any falsy value with the fallback, a real 0 included.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way