JS Built-ins
← Object144 / 150
ObjecttoStringintermediate

toString() β€” custom override

πŸ“– Usage Example
// Get string representation of an object
const obj = { a: 1 }
obj.toString()   // β†’ '[object Object]'
Object.prototype.toString.call([])   // β†’ '[object Array]'

Use toString() to convert an object to a string; calling it via Object.prototype.toString.call() reveals the internal type tag.

toString() β€” custom override

You can override toString() on your own objects to return a meaningful string representation.

Challenge: Create an object with a custom toString() method.

πŸ’‘ Hints (1)
  • β€’ Override toString to provide a meaningful string representation for your objects