Skip to content

JavaScript Symbols

A symbol is a primitive value that is guaranteed to be unique. Its main use is as an object property key that cannot clash with any other key, including keys added by other code or by future versions of the language.

Symbol() returns a new symbol every time it is called. The optional argument is a description, which helps when debugging but does not affect identity, so two symbols with the same description are still different values. Symbol is not a constructor, so new Symbol() throws a TypeError.

const id = Symbol('id');
const otherId = Symbol('id');
console.log(typeof id);
console.log(id === otherId);
console.log(id.description);
console.log(id.toString());

This prints:

symbol
false
id
Symbol(id)

A symbol key goes in square brackets, both in an object literal and when reading the property. Symbol-keyed properties are left out of Object.keys, for...in loops and JSON.stringify, so they suit data that belongs to your code rather than to the object’s public shape.

const internalId = Symbol('internalId');
const book = {
title: 'Field Guide to Mosses',
pages: 212,
[internalId]: 'BK-0042',
};
console.log(book[internalId]);
console.log(Object.keys(book));
for (const key in book) console.log(key);
console.log(JSON.stringify(book));
console.log(Object.getOwnPropertySymbols(book));
console.log(Reflect.ownKeys(book));

This prints:

BK-0042
[ 'title', 'pages' ]
title
pages
{"title":"Field Guide to Mosses","pages":212}
[ Symbol(internalId) ]
[ 'title', 'pages', Symbol(internalId) ]

The last two lines show that a symbol key is hidden, not private. Object.getOwnPropertySymbols and Reflect.ownKeys both find it, and Object.assign and the spread syntax copy it. For data that really must be private, use a class’s #private fields.

Symbol.for(key) looks up a symbol in a registry shared by the whole program, and creates it there if it does not exist yet. Two calls with the same key return the same symbol, which lets separate modules agree on one. Symbol.keyFor returns the key of a registered symbol, and undefined for a symbol made with Symbol().

const a = Symbol.for('app.theme');
const b = Symbol.for('app.theme');
console.log(a === b);
console.log(Symbol.keyFor(a));
console.log(Symbol.keyFor(Symbol('app.theme')));

This prints:

true
app.theme
undefined

JavaScript defines a set of symbols as static properties of Symbol, and the language itself looks for them on objects. The most used is Symbol.iterator, which makes an object work with for...of and the spread syntax.

const temperatures = {
readings: [12, 15, 9],
*[Symbol.iterator]() {
yield* this.readings;
},
};
for (const t of temperatures) console.log(t);
console.log([...temperatures]);

This prints:

12
15
9
[ 12, 15, 9 ]

Others include Symbol.toPrimitive, which controls how an object converts to a number or string, and Symbol.asyncIterator, which for await...of uses. Custom Iterators goes further with Symbol.iterator.

A symbol does not convert to a string implicitly, so using one in a template literal or with + throws. Call String() or .toString(), or read .description, instead.

const tag = Symbol('tag');
try {
console.log(`Tag: ${tag}`);
} catch (err) {
console.log(err.message);
}
console.log(String(tag));

This prints:

Cannot convert a Symbol value to a string
Symbol(tag)

Steve Griffith covers this topic in his video Intro to JavaScript Symbols.