Sub topic 10
Converting to Strings
This chapter assumes you know the seven primitive types, the difference between primitives and objects, and the difference between explicit and implicit coercion. Here you will learn the ToString operation, which is the rule set JavaScript uses whenever it needs a string, so you can predict the output for any input.
Where ToString Runs
ToString runs in explicit and implicit forms:
- Explicit:
String(value)orvalue.toString(). - Implicit: template literals (
${value}),+with a string operand, property keys (obj[1]uses the key"1"), and methods such asalert()orArray.prototype.join.
All of these follow the same rules, apart from one difference with symbols covered below.
The Rules by Type
| Input type | Result | Example |
|---|---|---|
| undefined | `"undefined"` | `String(undefined)` |
| null | `"null"` | `String(null)` |
| boolean | `"true"` or `"false"` | `String(false)` |
| number | decimal text, with special cases | `String(-0)` gives `"0"` |
| bigint | digits with no `n` suffix | `String(10n)` gives `"10"` |
| symbol | TypeError if implicit; `String()` gives `"Symbol(desc)"` | `String(Symbol("id"))` |
| object | ToPrimitive with hint "string", then ToString | `String([1, 2])` gives `"1,2"` |
Number Surprises
Numbers lose some detail when they become strings:
String(-0); // "0": the sign is dropped
String(NaN); // "NaN"
String(1e21); // "1e+21": exponent form from 1e21 upward
String(1e20); // "100000000000000000000"
String(0.000001); // "0.000001"
String(0.0000001); // "1e-7": exponent form below 1e-6
String(0.1 + 0.2); // "0.30000000000000004"Use Object.is if you need to detect -0. Use toFixed or Intl.NumberFormat for display formatting.
Objects and Arrays
Objects go through ToPrimitive, which the next chapter covers in detail. For plain objects and arrays, toString produces the result:
String({}); // "[object Object]"
String([1, [2, 3]]); // "1,2,3": nested arrays are joined too
String([1, null, undefined]); // "1,,": null and undefined become ""
String([]); // ""
String(new Date(0)).length > 0; // true: Date gives a readable date stringThe Symbol Trap
Symbols refuse to convert implicitly. This prevents a symbol from silently turning into a string key by accident:
const id = Symbol("id");
String(id); // "Symbol(id)"
id.toString(); // "Symbol(id)"
id.description; // "id"
// `${id}`; // TypeError: Cannot convert a Symbol value to a string
// "" + id; // TypeErrorExercise
What do String([0, -0, null, NaN]) and String(123n) return, and what happens with ${Symbol("x")}?
Show answer
String([0, -0, null, NaN]) returns "0,0,,NaN" because -0 becomes "0" and null becomes an empty string inside join. String(123n) returns "123" with no n suffix. The template literal throws a TypeError, since symbols cannot be converted to strings implicitly.
Exercise
Why is String(value) safer than value.toString() for logging an unknown value?
Show answer
value.toString() throws a TypeError when value is null or undefined. String(value) returns "null" or "undefined" for those values and also handles symbols without throwing.