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

JS Puzzles · Const

Number coercion table

Number coercion converts JavaScript values to numbers using type-specific rules. Numbers pass through, primitive values map predictably, strings are parsed, and some values produce NaN or throw.

How it works

JavaScript’s number coercion converts values according to their type: numbers stay unchanged, null and booleans map to numeric values, strings are parsed, and unsupported values can produce NaN or throw. Objects are first reduced to a primitive using their numeric conversion hooks.

Syntax

Number(value)

Parameters

NameTypeDescription
valueanyThe value to convert to a number.

Returns

A number primitive; conversion can produce NaN or throw TypeError for unsupported inputs.

Key points

  • A value already having the Number type passes through numeric coercion unchanged.
  • Converting undefined numerically produces NaN, indicating that it is not a valid numeric value.
  • Numeric coercion treats null as zero.
  • Boolean conversion uses one for true and zero for false.
  • A string containing no non-whitespace characters becomes zero.
  • Object conversion has two stages: obtain a primitive using the prescribed hooks, then apply numeric conversion.

Common mistakes

  • Implicit numeric coercion rejects BigInt instead of silently risking precision loss; use Number(value) only when that conversion is intentional.
  • String inputs containing numeric separators such as "1_000" are not accepted by number coercion.
  • A Symbol cannot be converted to a number, so coercion must be avoided or handled before conversion.
  • Failed string parsing does not throw under ordinary Number conversion; it produces NaN.

Example: Inspect number coercion

const values = [undefined, null, true, false, "", "  ", "42", 3.14];
console.log(values.map(value => [String(value), Number(value)]));

Run this to inspect how representative JavaScript values become numbers.

When to use it

Convert numeric strings

Normalize form, query, or configuration text before arithmetic.

Validate parsed values

Detect invalid numeric input after conversion.

Debug coercion behavior

Understand implicit conversions in arithmetic and built-in APIs.

Interview questions

QHow does JavaScript’s number coercion map common primitive values?

Number coercion leaves numbers unchanged, maps undefined to NaN, null to 0, true to 1, and false to 0. Empty or whitespace-only strings also become 0, while numeric strings are parsed.

Follow-up: What happens to a BigInt? Number coercion throws TypeError, whereas Number(bigInt) converts it and may lose precision.

QCompare unary plus with Number(value).

Unary plus performs the same number-coercion steps as most built-ins that expect numbers. Number(value) follows the same process, except it converts BigInt rather than throwing.

Follow-up: Why can Number(bigInt) be dangerous? A Number may not preserve the BigInt’s exact value when it exceeds the safe integer range.

QHow are objects converted during number coercion?

For objects, JavaScript first obtains a primitive by trying Symbol.toPrimitive with the number hint, then valueOf(), then toString(). It converts the resulting primitive to a number.

Follow-up: What if all conversion methods fail to provide a usable primitive? The object-to-primitive operation throws rather than completing numeric conversion.

  • Number()
  • unary plus (+)
  • Symbol.toPrimitive
  • valueOf()
  • toString()
  • BigInt

Source: Number - JavaScript | MDN. Checked 2026-10-15.