Symbol

A Symbol primitive is used to represent an entirely unique value. Symbols are created via invoking the Symbol function, like so:

const totallyUniqueKey = Symbol();

You can optionally pass an initial argument to this function to annotate your symbol for your own debugging purposes, but this is not necessary:

const totallyUniqueKey = Symbol('My Special Key');

Symbols are used to act as property keys where uniqueness is required or where you want to store metadata on objects. When you add a property to an object with a Symbol key, it will not be iterated over by normal object iteration approaches (such as for...in). Symbol keys of an object can only be retrieved via Object.getOwnPropertySymbols:

const thing = {};thing.name = 'James'; ...

Get Clean Code in JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.