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

JS Puzzles · Const

Type Coercion

JavaScript automatically converts values when an operation expects another type. The conversion path depends on the context and can involve primitive, numeric, BigInt, string, or object-to-primitive rules.

How it works

JavaScript may implicitly convert a value when an operation expects another type. The conversion rules depend on the target context: primitive, numeric, BigInt, or string conversion; objects first try Symbol.toPrimitive and then valueOf() or toString().

Key points

  • JavaScript commonly accepts a value in a context requiring another type and converts it automatically.
  • Primitive coercion applies when an operation needs a primitive but does not strongly require string, number, or BigInt.
  • Numeric coercion distinguishes the Number and BigInt numeric types.
  • For objects, Symbol.toPrimitive is consulted before valueOf() and toString().
  • String conversion prefers Symbol.toPrimitive with a string hint, then toString(), then valueOf().

Common mistakes

  • Implicit conversion can make an expression use a different type than the operand visibly suggests.
  • With +, JavaScript can choose string concatenation instead of numeric addition when one operand becomes a string.
  • A custom Symbol.toPrimitive method must return a primitive, not another object.

Example: Explicit conversion avoids ambiguous + behavior

const values = ["5", 9, null, undefined, true];

for (const value of values) {
  console.log(String(value), Number(value));
}

console.log("5" + 9); // "59"
console.log(Number("5") + 9); // 14

The first loop shows explicit string and number conversion. The final lines contrast string concatenation caused by + with explicit numeric conversion.

When to use it

Normalize numeric input

Parsing user input or form values before arithmetic.

Handle flexible inputs

Building APIs that accept values from multiple primitive types.

Customize object conversion

Defining custom object behavior for arithmetic or interpolation.

Interview questions

QWhat is type coercion in JavaScript?

Type coercion is the automatic conversion of a value from one type to another. JavaScript permits this because it is weakly typed, so operations can convert mismatched operands instead of immediately throwing a type error.

Follow-up: How does object conversion begin? JavaScript first checks Symbol.toPrimitive, then falls back to valueOf() and toString() according to the conversion hint.

QWhy can + produce concatenation rather than addition?

The + operator can concatenate when string conversion is selected, so a numeric operand may become text. Explicit Number(value) conversion makes numeric addition intentional and avoids relying on that choice.

Follow-up: What happens with Number(null), Number(undefined), and Number(true)? They become 0, NaN, and 1 respectively.

QHow are objects coerced to primitive values?

Object-to-primitive conversion uses Symbol.toPrimitive first. If it is absent, JavaScript tries valueOf() and toString() in the order determined by the hint.

Follow-up: For a string hint, the order is toString() followed by valueOf(); for numeric conversion, the preferred order is Symbol.toPrimitive("number"), valueOf(), then toString().

  • Primitive coercion
  • Numeric coercion
  • String coercion
  • Symbol.toPrimitive
  • valueOf()
  • toString()

Source: JavaScript data types and data structures. Checked 2026-10-15.