Symbol.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

La méthode toString() renvoie une chaîne de caractères représentant l'objet Symbol.

Exemple interactif

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

Syntaxe

js
Symbol().toString();

Valeur de retour

Une chaîne de caractères qui représente l'objet Symbol.

Description

L'objet Symbol surcharge la méthode toString() d'Object et n'hérite pas de Object.prototype.toString(). Pour les objets Symbol, la méthode toString() renvoie représentation de l'objet sous forme d'une chaîne de caractères.

Concaténation de chaînes et symboles

Bien qu'il soit possible d'appeler toString() pour les symboles, il n'est pas possible de concaténer une chaîne de caractères avec ce type d'objet :

js
Symbol("toto") + "machin"; // TypeError : Impossible de convertir un symbole en chaîne de caractères

Exemples

js
Symbol("desc").toString(); // "Symbol(desc)"

// symboles connus
Symbol.iterator.toString(); // "Symbol(Symbol.iterator)

// symboles globaux
Symbol.for("toto").toString(); // "Symbol(toto)"

Spécifications

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

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
toString

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Voir aussi