JavaScript Tutorial · JavaScript Basics · Lesson 10 of 44

JavaScript Booleans

true, false, comparisons and truthy and falsy values.

Boolean values

A boolean is either true or false. Comparisons produce booleans:

Example 1Try it yourself
const age = 20;
console.log(age >= 18);
console.log(age === 21);
console.log(10 > 9 && 2 > 3);

Output

true
false
false

Truthy and falsy

In an if condition, every value counts as true or false. These six values are falsy:

false, 0, "" (empty string), null, undefined, NaN

Everything else is truthy — including "0", "false", [] and {}!

Example 2Try it yourself
const values = { '0': 0, '""': "", '"0"': "0", '"hello"': "hello", null: null,
                 undefined: undefined, NaN: NaN, "[]": [], "{}": {} };
for (const [label, v] of Object.entries(values)) {
  console.log(label.padEnd(9), Boolean(v) ? "truthy" : "falsy");
}

Output

0         falsy
""        falsy
"0"       truthy
"hello"   truthy
null      falsy
undefined falsy
NaN       falsy
[]        truthy
{}        truthy

Convert any value to a boolean with Boolean(value) or !!value.

Test yourself

Which of these values is truthy?

  1. 0
  2. ""
  3. "0"
  4. null
Show answer

Answer: "0"

Any non-empty string is truthy, even "0".