Standard built-ins

Symbol.toPrimitive

Try the free JavaScript compiler

Key for the method that converts an object into a primitive according to the hint string. An object result throws TypeError.

Formal Syntax & Structure

javascript syntaxjavascript
Symbol.toPrimitive

Return Value

symbol

The predefined protocol key.

Working Example

Example Snippetjavascript
const v={[Symbol.toPrimitive](h){return h==="string"?"unit":4;}};console.log(String(v),+v);
JavaScript Reference