What are JavaScript Symbols and When to Use Them?

Bartosz Salwiczek
4 min readDec 11, 2022

Symbols are a useful tool in your JavaScript toolset. I believe that every developer should understand what symbols are at least because they are very common in external libraries. Even if you never use them, there are people who find them useful and you should be able to understand their code.

What are symbols?

Symbols are the primitive type introduced to Javascript in ECMAScript 6. Their primary purpose is to have a unique result every time they are constructed. You can think of symbols as special tokens that guarantee uniqueness (like UUID).

There are two types of symbols. I will call them scoped and global.

Scoped symbols

Scoped symbols are the ones created with a Symbol() factory function. They are constructed similarly to objects. By calling a factory you will get a reference to the new symbol.

const symbol = Symbol();
console.log(symbol); // Symbol()

Optionally you can provide an argument to the factory function. It will be treated as the name of the symbol used only for debugging purposes.

const symbolWithName = Symbol("name");
console.log(symbolWithName); // Symbol(name)

--

--

Bartosz Salwiczek

I write about Software Engineering. JS/TS/Node/Vue/React/Flutter. Get access to all my articles by joining Medium https://medium.com/@bsalwiczek/membership