Object labels with Symbol.toStringTag
Quick answer
Symbol.toStringTag influences the label returned by Object.prototype.toString. It is a display convention, not a reliable proof of built-in identity.
How it works
Object.prototype.toString.call(value) produces a bracketed tag such as [object Object]. If the retrieved Symbol.toStringTag property is a string, it can replace the default label. An inherited tag works too. Internal-slot checks performed by built-in methods are not satisfied by a cosmetic tag; Array.isArray provides a different kind of check.
const x={[Symbol.toStringTag]:"Inventory"}; console.log(Object.prototype.toString.call(x));Expected output in Node.js:
[object Inventory]A string-valued tag customizes the label.
const x={[Symbol.toStringTag]:"Array"}; console.log(Object.prototype.toString.call(x),Array.isArray(x));Expected output in Node.js:
[object Array] falseA cosmetic tag cannot create array identity.
Common mistakes
Never authenticate or validate a value merely by comparing its toStringTag. Use appropriate type checks and validate required behavior.
Practice
Compare an ordinary object tagged Array with a real array using Array.isArray and the display tag.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Object labels with Symbol.toStringTag — Knowledge Check
Reason about the protocol or programming technique and predict the example output.
Which method directly consults a string toStringTag?
Can an inherited toStringTag influence the label?
Does a tag of Map provide Map internal slots?
What is the exact console output in Object labels with Symbol.toStringTag example 1?
const x={[Symbol.toStringTag]:"Inventory"}; console.log(Object.prototype.toString.call(x));What is the exact console output in Object labels with Symbol.toStringTag example 2?
const x={[Symbol.toStringTag]:"Array"}; console.log(Object.prototype.toString.call(x),Array.isArray(x));