Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Boolean coercion

JavaScript coerces values to booleans in conditions and other Boolean contexts. Only a small set of values is falsy; every other value is truthy.

How it works

JavaScript converts values to booleans whenever an operation requires a Boolean, including conditions and loops. The falsy values are false, 0, -0, 0n, "", null, undefined, NaN, and document.all; every other value is truthy. Boolean(value) or !!value performs the same conversion explicitly.

Syntax

Boolean(value)

Returns

A Boolean primitive when conversion is explicitly requested with Boolean(value) or !!value; Boolean contexts use the converted result for their operation.

Key points

  • Conditions and loops automatically convert their tested expressions to Boolean values.
  • Only the documented falsy values convert to false; all other values convert to true.
  • Boolean(value) is an explicit way to obtain a Boolean primitive.
  • && uses truthiness for control flow and returns an operand rather than necessarily returning true or false.

Common mistakes

  • An empty array or object is truthy, even though it may appear empty.

Example: Converting values to booleans

const values = [false, 0, "", null, undefined, NaN, "0", [], {}];
for (const value of values) {
  console.log(value, Boolean(value));
}

Boolean(value) makes the implicit truthiness conversion explicit for each value.

When to use it

Conditional control flow

Guard an if statement or loop with a value whose presence or state should control execution.

Explicit Boolean normalization

Normalize an arbitrary value into an actual Boolean for an API, stored state, or return value.

Defaulting and conditional selection

Use short-circuit expressions to select values based on their truthiness.

Interview questions

QWhat is Boolean coercion?

JavaScript applies Boolean coercion in Boolean contexts such as conditionals and loops. Values such as false, 0, an empty string, null, undefined, and NaN become false; other values become true.

Follow-up: How can coercion be made explicit? Use Boolean(value) or the double-NOT operator !!value; both produce a Boolean primitive.

QWhich values are falsy?

The falsy set includes false, 0, -0, 0n, "", null, undefined, NaN, and document.all. Every other JavaScript value is truthy.

Follow-up: Is [] falsy because it is empty? No. Arrays and other objects are truthy, regardless of whether they contain elements.

QDoes a logical operator always return true or false?

Logical operators use truthiness but do not necessarily return a Boolean. For example, && returns the first falsy operand, or the last operand when all operands are truthy.

Follow-up: What does || return? It returns the first truthy operand, or the final operand if all operands are falsy.

  • truthy
  • falsy
  • Boolean()
  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)

Source: Truthy - Glossary | MDN. Checked 2026-10-15.