ToPrimitive — how an object becomes a number or a string¶
Level: 301 · for anyone who has wondered why an object prints as [object Object]
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: When an operator needs a primitive from an object it passes a hint — "number", "string" or "default" — to Symbol.toPrimitive, or else tries valueOf then toString (a string hint reverses the order); Date treats "default" as string, so date + 1 concatenates.
Keywords: ToPrimitive, Symbol.toPrimitive, valueOf, toString
What the finished page will answer¶
- Which hint do unary
+, a template literal,+with a string, and==pass toSymbol.toPrimitive? - For an object with both
valueOfandtoString, what doobj + 1,`${obj}`andobj * 2return? - Why is
typeof (new Date(0) + 1)"string"whiletypeof (new Date(0) - 1)is"number"? - What happens when every conversion method returns an object, and when the object has no prototype at all?
Examples it will need¶
-
toprimitive_hints_js.js— the hint each operator passes, logged from inside Symbol.toPrimitive -
toprimitive_valueof_tostring_js.js— an object with valueOf and toString through six operators, then a Date through + and - -
toprimitive_failure_js.js— the TypeError for an object whose conversions all return objects, and for Object.create(null)
See also¶
- Well-known symbols —
Symbol.toPrimitiveamong the language's other hooks - The
+operator — the operator that passes the default hint ===and==—==converts an object this way too- Primitives and wrappers — wrapper objects converting back to their primitives
- Rust: Debug and Display ↗ — in Rust, two text forms, and only one is generated for you