Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Symbol.prototype.toString()

BaselineWidely available

ThetoString() method ofSymbol values returns a string representing this symbol value.

Try it

console.log(Symbol("desc").toString());// Expected output: "Symbol(desc)"console.log(Symbol.iterator.toString());// Expected output: "Symbol(Symbol.iterator)console.log(Symbol.for("foo").toString());// Expected output: "Symbol(foo)"// console.log(Symbol('foo') + 'bar');// Expected output: Error: Can't convert symbol to string

Syntax

js
toString()

Parameters

None.

Return value

A string representing the specified symbol value.

Description

TheSymbol object overrides thetoString method ofObject; it does not inheritObject.prototype.toString(). ForSymbol values, thetoString method returns a descriptive string in the form"Symbol(description)", wheredescription is the symbol'sdescription.

ThetoString() method requires itsthis value to be aSymbol primitive or wrapper object. It throws aTypeError for otherthis values without attempting to coerce them to symbol values.

BecauseSymbol has a[Symbol.toPrimitive]() method, that method always takes priority overtoString() when aSymbol object iscoerced to a string. However, becauseSymbol.prototype[Symbol.toPrimitive]() returns a symbol primitive, and symbol primitives throw aTypeError when implicitly converted to a string, thetoString() method is never implicitly called by the language. To stringify a symbol, you must explicitly call itstoString() method or use theString() function.

Examples

Using toString()

js
Symbol("desc").toString(); // "Symbol(desc)"// well-known symbolsSymbol.iterator.toString(); // "Symbol(Symbol.iterator)"// global symbolsSymbol.for("foo").toString(); // "Symbol(foo)"

Implicitly calling toString()

The only way to make JavaScript implicitly calltoString() instead of[Symbol.toPrimitive]() on a symbol wrapper object is bydeleting the[Symbol.toPrimitive]() method first.

Warning:You should not do this in practice. Never mutate built-in objects unless you know exactly what you're doing.

js
delete Symbol.prototype[Symbol.toPrimitive];console.log(`${Object(Symbol("foo"))}`); // "Symbol(foo)"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-symbol.prototype.tostring

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp