boolean

Contents

boolean#

true / false. The falsy values are false, 0, -0, 0n, "", null, undefined, NaN; everything else is truthy. The operator uses !!x or Boolean(x) to coerce explicitly.

Empty containers are truthy; only the eight falsy values above make if (x) skip the body.

Boolean([]);          // true (empty array is truthy)
Boolean({});          // true (empty object is truthy)

The string "0" is non-empty, so it is truthy.

Boolean("0");         // true

Double-negate for an explicit coercion to boolean.

!!undefined;          // false

References#

  • Casting for == vs === coercion rules.

  • Control flow for if conditions and short-circuit && / || / ??.