JavaScript's Symbol Type (ECMAScript 6)

JavaScript is not a strongly-typed language like many classic compiled languages. Instead, like many scripting languages, it is loosely typed and has just a few basic types: number, string, object, boolean. There are also two types which are used as values (rather than to hold varying values like numbers and strings) which are null (no value) and undefined (the initial or undefined value of a variable). JavaScript is aware of Arrays as a type of object, but is still an object and not a separate type. Objects in JavaScript take prototypical inheritence. A literal object looks like a Python dict.

The ECMAScript 6 standard (JavaScript is ECMAScript) adds a new type called 'symbol'. A symbol is another way to identify object properties, which we are used to referring to by JavaScript identifier strings (which use a limited character set). For example, given object obj with a property prop we can refer to it like so: obj.prop. A symbol can have a string describing it, and the string can have spaces and other special characters not found in a normal object property identifier. If we have a Symbol symb and it is used as a property of obj, we can refer to obj's symb like so: obj[symb].

There is no literal Symbol representation like 10 is a literal number and 'ten' is a literal string. You just need to know that creating a Symbol gives it a unique value. You explicitly create symbols with Symbol() or Symbol('optional description') (but not by using new Symbol()).

Symbols are used to identify an object property, like literal strings do. But they lie in a separate namespace (Object.getOwnPropertySymbols()) from literal string identifiers (Object.getOwnPropertyNames()). They won't overlap or override each other and you have to use the two separate methods above to get the lists of them. You can create many symbols with the same or no description and they are all unique and can all be used in an object as distinct keys.

Use Symbols when you want to add new properties to objects without possibly conflicting with existing (string identifier) properties.