JavaScript ยท Chapter 27 of 55

JavaScript Booleans

A boolean represents one of two values: `true` or `false`. Booleans are the result of comparisons and logical operations, and drive decision-making in conditionals.

Every JavaScript value has an inherent 'truthiness' โ€” falsy values include `0`, `''`, `null`, `undefined`, `NaN`, and `false` itself; everything else is truthy.

Falsy values

Only six values are falsy: false, 0, '', null, undefined, and NaN. Everything else โ€” including '0' the string, and empty objects/arrays โ€” is truthy.

Boolean() conversion

`Boolean(value)` explicitly converts any value to true or false following truthiness rules, useful for validation checks.

Example 1 (javascript)
console.log(Boolean(0));
console.log(Boolean(""));
console.log(Boolean("hello"));
Output
false
false
true

Boolean() reveals a value's truthiness.

Example 2 (javascript)
console.log(Boolean([]));
console.log(Boolean({}));
Output
true
true

Empty arrays and objects are truthy, unlike empty strings.

Key points

  • Booleans are `true` or `false`.
  • Falsy values: false, 0, '', null, undefined, NaN.
  • Empty arrays and objects are truthy.
  • Boolean(value) converts any value using truthiness rules.
๐Ÿ’ก Note: A common bug is assuming empty arrays/objects are falsy โ€” they are not in JavaScript.

๐Ÿ“ Quick Quiz

1. Which of these is falsy?

2. Is an empty array truthy or falsy?

3. How many values are falsy in JS?