JavaScript Symbols
A Symbol represents a unique identifier.
Unique Values
A Symbol represents a unique identifier.
Every Symbol value is unique, even if they have the same description.
A Symbol is a hidden identifier that no code can accidentally access.
Creating Symbols
Use Symbol() to create a Symbol:
Example
Example Code
javascriptconst id1 = Symbol();
const id2 = Symbol();The two Symbols are different:
Example
Example Code
javascriptconst id1 = Symbol();
const id2 = Symbol();
let result = (id1 === id2);Symbol Descriptions
You can add a description to a Symbol:
Example
Example Code
javascriptconst id = Symbol("id");The description is only for debugging and readability.
It does not make the Symbol unique.
Example
Example Code
javascriptconst id1 = Symbol("id");
const id2 = Symbol("id");
let result = (id1 === id2);Using Symbols as Object Keys
Symbols are often used as object property keys.
This lets you create hidden or unique properties.
Example
Example Code
javascriptconst id = Symbol("id");
const person = {
firstName: "John",
lastName: "Doe"
};
person[id] = 123;The Symbol Type
A JavaScript Symbol is a primitive data type just like Number, String, or Boolean.
The JavaScript type of a Symbol is symbol:
Example
Example Code
javascriptconst id = Symbol("id");
let type = typeof id;Hidden Identifiers
A Symbol represents a unique "hidden" identifier that no code can accidentally access.
For instance, if different programmers want to add a person.id property to a person object belonging to a third-party code, they could mix each others values.
Using Symbol() to create a unique identifiers, solves this problem:
Example
Example Code
javascriptconst person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
let id = Symbol('id');
person[id] = 140353;
// Now person[id] = 140353
// but person.id is still undefinedSymbols are Not Included in for...in
Symbol properties are ignored by for...in loops:
Example
Example Code
javascriptconst id = Symbol("id");
const person = {
firstName: "John",
lastName: "Doe"
};
person[id] = 123;
let text = "";
for (let x in person) {
text += x + " ";
}
// Display textSymbols are Not Included in JSON
Symbol properties are ignored by JSON.stringify():
Example
Example Code
javascriptconst id = Symbol("id");
const person = {
name: "John"
};
person[id] = 123;
let text = JSON.stringify(person);Global Symbols
JavaScript has a global Symbol registry.
Use Symbol.for() to create or reuse a global Symbol:
Example
Example Code
javascriptconst id1 = Symbol.for("id");
const id2 = Symbol.for("id");
let result = (id1 === id2);Well-Known Symbols
JavaScript provides built-in Symbols called well-known Symbols.
These Symbols control JavaScript behaviors.
Examples:
- Symbol.iterator
- Symbol.asyncIterator
- Symbol.toStringTag
- Symbol.toPrimitive
Symbol.iterator
The Symbol.iterator Symbol defines an iterable object.
This makes objects work with loops like for...of.
Example
Example Code
javascriptconst myObject = {
data: ["A", "B", "C"],
[Symbol.iterator]() {
let index = 0;
let data = this.data;
return {
next() {
if (index ";
}
// Display textWhen to Use Symbols
Use Symbols when you need:
- Unique object property names
- Hidden object properties
- Custom iterable objects
- Special object behavior
When NOT to Use Symbols
Symbols are not commonly used in everyday JavaScript code.
Do not use Symbols unless you need unique property keys or advanced object behavior.
For normal object properties, use strings names instead.
Browser Support
Symbol is an ES6 feature.
ES6 is fully supported in all modern browsers since June 2017:
| Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
| May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |
Interactive Code Examples
JS Example 1
javascriptconst id1 = Symbol();
const id2 = Symbol();JS Example 2
javascriptconst id1 = Symbol();
const id2 = Symbol();
let result = (id1 === id2);JS Example 3
javascriptconst id = Symbol("id");JS Example 4
javascriptconst id1 = Symbol("id");
const id2 = Symbol("id");
let result = (id1 === id2);JS Example 5
javascriptconst id = Symbol("id");
const person = {
firstName: "John",
lastName: "Doe"
};
person[id] = 123;JS Example 6
javascriptconst id = Symbol("id");
let type = typeof id;