© Russ Ferguson and Keith Cirkel 2017

Russ Ferguson and Keith Cirkel, JavaScript Recipes, 10.1007/978-1-4302-6107-0_15

15. Working with Symbols

Russ Ferguson and Keith Cirkel2

(1)Ocean, New Jersey, USA

(2)London, UK

What Are the Advantages of Using Symbols ?

Problem

You want to know why and how to use symbols in a project.

Solution

Symbols give you the ability to create unique tokens that will never clash with other symbols. Similar to UUIDs (Universal Unique Identifiers) , symbols can be used on objects to make unique properties.

The Code

Listing 15-1. Creating Symbols
let mySym1 = Symbol('This is my first Symbol');let mySym2 = Symbol(); //returns falseconsole.log(mySym1 == mySym2);console.log(mySym1.toString()) //returns  Symbol(This is my first Symbol) ...

Get JavaScript Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.