Symbol is a new primitive type that has been added to ES6 (other primitive types are Number, String, Boolean, Object, Null, Undefined). It expects a descriptor and doesn’t have a literal form.

See the Pen thecodelog.com - ES6 Symbols 1 by Deano (@deangilewicz) on CodePen.

The main point of a Symbol is to create a string-like value that can’t collide with any other value – they are unique identifiers that help avoid naming collisions. No two Symbols are the same.

See the Pen thecodelog.com - ES6 Symbols 2 by Deano (@deangilewicz) on CodePen.

Symbols can be useful when used as keys in objects.

See the Pen thecodelog.com - ES6 Symbols 3 by Deano (@deangilewicz) on CodePen.

However, Symbols are not enumerable – e.g. you cannot loop over them. This could be useful when you want to store private data.

See the Pen thecodelog.com - ES6 Symbols 4 by Deano (@deangilewicz) on CodePen.

You can use Object.getOwnPropertySymbols() to get the Symbol keys and use the keys in a map function to get the values.

See the Pen thecodelog.com - ES6 Symbols 5 by Deano (@deangilewicz) on CodePen.

Symbol.for() is a method that looks in the global symbol registry to see if a symbol is already stored with the provided description text, and returns it if so. If not, it creates one to return.

See the Pen thecodelog.com - ES6 Symbols 6 by Deano (@deangilewicz) on CodePen.

To retrieve a registered symbol’s description text (key) you can use Symbol.keyFor().

See the Pen thecodelog.com - ES6 Symbols 7 by Deano (@deangilewicz) on CodePen.

ES6 comes with a number of predefined built-in symbols. The most common ones being: @@iterator, @@toString Tag, @@toPrimitive.

See the Pen thecodelog.com - ES6 Symbols 8 by Deano (@deangilewicz) on CodePen.

We can create a custom iterator using Symbol.iterator.

See the Pen thecodelog.com - ES6 Symbols 9 by Deano (@deangilewicz) on CodePen.

You make an iterator an iterable by giving it a Symbol.iterator method that simply returns the iterator itself:

See the Pen thecodelog.com - ES6 Symbols 10 by Deano (@deangilewicz) on CodePen.