JS TutorialLesson #103

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

javascript
Try it Yourself
const id1 = Symbol();

const id2 = Symbol();

The two Symbols are different:

Example

Example Code

javascript
Try it Yourself
const id1 = Symbol();

const id2 = Symbol();

let result = (id1 === id2);
The result is false. > Every Symbol has a unique value.

Symbol Descriptions

You can add a description to a Symbol:

Example

Example Code

javascript
Try it Yourself
const id = Symbol("id");

The description is only for debugging and readability.

It does not make the Symbol unique.

Example

Example Code

javascript
Try it Yourself
const id1 = Symbol("id");

const id2 = Symbol("id");

let result = (id1 === id2);
The result is still false. > Symbols are always unique. > If you create two symbols with the same description they will have different values: > Symbol("id") == Symbol("id"); // false

Using Symbols as Object Keys

Symbols are often used as object property keys.

This lets you create hidden or unique properties.

Example

Example Code

javascript
Try it Yourself
const 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

javascript
Try it Yourself
const 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

javascript
Try it Yourself
const 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 undefined

Symbols are Not Included in for...in

Symbol properties are ignored by for...in loops:

Example

Example Code

javascript
Try it Yourself
const id = Symbol("id");

const person = {

  firstName: "John",

  lastName: "Doe"

};

person[id] = 123;

let text = "";

for (let x in person) {

  text += x + " ";

}

// Display text
The Symbol property is not displayed.

Symbols are Not Included in JSON

Symbol properties are ignored by JSON.stringify():

Example

Example Code

javascript
Try it Yourself
const 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

javascript
Try it Yourself
const id1 = Symbol.for("id");

const id2 = Symbol.for("id");

let result = (id1 === id2);
The result is true.

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

javascript
Try it Yourself
const myObject = {

  data: ["A", "B", "C"],

  [Symbol.iterator]() {

    let index = 0;

    let data = this.data;

    return {

      next() {

        if (index  ";

}

// Display text

When 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 51Edge 15Firefox 54Safari 10Opera 38
May 2016Apr 2017Jun 2017Sep 2016Jun 2016

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const id1 = Symbol();

const id2 = Symbol();

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const id1 = Symbol();

const id2 = Symbol();


let result = (id1 === id2);

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const id = Symbol("id");

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const id1 = Symbol("id");

const id2 = Symbol("id");


let result = (id1 === id2);

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const id = Symbol("id");


const person = {

  firstName: "John",

  lastName: "Doe"

};


person[id] = 123;

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const id = Symbol("id");



let type = typeof id;