Sub topic 7
What Type Coercion Is: Explicit and Implicit Conversion
This chapter assumes you know the seven primitive types, the difference between primitives and objects, and how Number edge cases such as NaN behave. Here you get the overall picture of conversion. Each later chapter covers one conversion rule in detail.
Two Ways a Value Changes Type
Type coercion means converting a value from one type to another. JavaScript does it in two ways:
- Explicit coercion is a conversion you write on purpose, such as
Number(x),String(x), orBoolean(x). - Implicit coercion is a conversion the language performs because an operator or statement needs a particular type. For example,
"3" - 1gives2because-only works on numbers.
Both kinds use the same internal rules. The only difference is whether the conversion is visible in your code.
const input = "42";
// Explicit: the conversion is visible
const n1 = Number(input); // 42
const s1 = String(42); // "42"
const b1 = Boolean(input); // true
// Implicit: the operator forces the conversion
const n2 = input * 1; // 42
const s2 = 42 + ""; // "42"
const b2 = !!input; // true
console.log(n1 === n2, s1 === s2, b1 === b2); // true true trueThe Abstract Operations
The ECMAScript specification describes conversion as a small set of internal procedures called abstract operations. You cannot call them directly, but every conversion goes through them:
| Operation | Produces | Triggered by (examples) | Column | Column |
|---|---|---|---|---|
| ToBoolean | `true` or `false` | `if`, `while`, `!`, `&&`, `\ | \ | `, `Boolean()` |
| ToNumber | a number | `-`, `*`, `/`, unary `+`, `<`, `Number()` | ||
| ToString | a string | template literals, `+` with a string, `String()` | ||
| ToPrimitive | a primitive | any of the above applied to an object |
ToPrimitive runs first whenever the input is an object. It turns the object into a primitive, and then ToNumber or ToString finishes the job. ToBoolean is the exception: it never calls ToPrimitive, because every object is truthy.
Why the Distinction Matters
Implicit coercion is not wrong in itself. Writing if (items.length) is idiomatic and clear. Bugs show up when code depends on a conversion the reader cannot see, or when an operator behaves in more than one way. The + operator is the main example of the second case:
const qty = "2"; // e.g. from a form field
console.log(qty + 1); // "21": string concatenation
console.log(qty - 1); // 1: numeric subtraction
console.log(Number(qty) + 1); // 3: explicit and predictableCommon Mistakes
- Assuming
+always adds. If either operand is a string after ToPrimitive,+concatenates. - Assuming an object becomes
falsein a condition. Empty arrays and empty objects are truthy. - Treating
parseIntandNumberas interchangeable. They follow different rules, which the ToNumber chapter covers.
Exercise
For each expression, say whether the coercion is explicit or implicit and which abstract operation does the main work: (a) String(7), (b) if ("") {}, (c) "5" * 2, (d) ` ${[1, 2]} `.
Show answer
(a) Explicit, ToString. (b) Implicit, ToBoolean (the empty string is falsy). (c) Implicit, ToNumber, giving 10. (d) Implicit: ToPrimitive turns the array into "1,2", then ToString gives "1,2".
Exercise
Why does Boolean([]) return true while Number([]) returns 0?
Show answer
ToBoolean does not call ToPrimitive and treats every object as truthy. ToNumber first calls ToPrimitive, which turns [] into "", and "" converts to 0.