JS Built-ins
SymboltoPrimitiveintermediate

Symbol.toPrimitive β€” string hint

πŸ“– Usage Example
const tag = {
  [Symbol.toPrimitive](hint: string) {
    return hint === 'string' ? 'Hello' : 0
  }
}
`${tag}`   // β†’ 'Hello'

Template literals trigger the 'string' hint, calling [Symbol.toPrimitive] with hint === 'string'.

Custom Coercion with String Hint

Template literals trigger the 'string' hint when an object has [Symbol.toPrimitive].

Challenge: Verify that template literal coercion triggers the string hint.

πŸ’‘ Hints (1)
  • β€’ Template literals use the string hint.