JS Built-ins
← Object145 / 150
ObjecttoStringintermediate

toString() β€” Object.prototype.toString.call(42)

πŸ“– 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.

Object.prototype.toString.call() β€” number type tag

Using Object.prototype.toString.call(42) returns "[object Number]", regardless of whether 42 is a primitive or a Number object.

Challenge: Use the toString type-tag to identify various value types.

πŸ’‘ Hints (1)
  • β€’ Object.prototype.toString.call works on primitives too, boxing them internally