Home

SymboltoStringTag

Symbol.toStringTag is a well-known symbol introduced in ECMAScript 2015 that customizes the result of Object.prototype.toString. It provides a way to influence the tag used in the default string representation of objects.

How it works: Object.prototype.toString returns a string like [object Type], where Type reflects the object's internal

Defining and usage: You can set Symbol.toStringTag on an object or its prototype to affect all instances.

Notes: The Symbol.toStringTag property is not enumerable and does not affect JSON serialization or other stringification

See also: Object.prototype.toString, Symbol, well-known symbols.

tag.
If
the
object
has
a
property
keyed
by
Symbol.toStringTag
whose
value
is
a
string,
that
string
is
used
as
the
Type.
For
example,
Object.prototype.toString.call(obj)
yields
[object
Custom]
when
obj[Symbol.toStringTag]
=
'Custom'.
If
the
property
is
absent
or
not
a
string,
the
default
internal
tag
is
used.
Example:
const
o
=
{
[Symbol.toStringTag]:
'MyType'
};
Object.prototype.toString.call(o);
//
"[object
MyType]".
For
classes,
assigning
on
the
prototype
also
affects
instances.
Class
C
{},
C.prototype[Symbol.toStringTag]
=
'C';
Object.prototype.toString.call(new
C());
//
"[object
C]".
methods;
it
specifically
alters
toString
output.
It
is
a
symbol-keyed
property,
retrievable
via
Object.getOwnPropertySymbols,
but
not
usually
visible
in
ordinary
property
enumerations.