A new primitive data type was introduced in ES2015 - Symbol. It’s relative new, you might not be familiar with it yet.
In this article we will explain what a Symbol is and how it can be used in real life apps.
What is symbol?
The data type symbol is a primitive data type, it is one of JavaScript’s 6 primitive data types: string, number, boolean, undefined, bigint and symbol.
There is a Symbol()
function available, it returns a value of type symbol. Every value returned is unique, you can never create two identical values, even though calling this function with the same parameter.
Symbol can accept an optional description as parameter, but only for debugging purpose.
1 | const symbol1 = Symbol(); |
In case you want to get an already created symbol, you can use Symbol.for()
method. This method creates a symbol available in a global symbol registry list if the given key was not found, otherwise the existing one was returned.
1 | Symbol.for('a'); // create a new global symbol |
Use cases
1.Used as an identifier for object properties
By using symbols, you don’t need to worry about key collisions in objects, this is the data type’s primary purpose.
1 | let thirdPartyObject = { |
2. Enable private properties.
The original motivation for introducing symbols is to enable private properties in JavaScript.
Symbols are not enumerable in for…in iterations.
1 | let obj = {} |
Symbol-keyed properties are ignored when using JSON.stringify() as well:
1 | JSON.stringify({[Symbol('foo')]: 'foo'}) |
However, they are not 100% hidden, you can still get these properties, use Object.getOwnPropertySymbols()
or Reflect.ownKeys()
:
1 | let obj = {} |
Predefined Symbols
It has several properties that expose several members of built-in object. These are predefined symbols, they are also called well-known symbols.
- Symbol.asyncIterator - A method that returns the default AsyncIterator for an object. Used by for await…of.
- Symbol.hasInstance - A method determining if a constructor object recognizes an object as its instance. Used by instanceof.
- Symbol.isConcatSpreadable - A Boolean value indicating if an object should be flattened to its array elements. Used by Array.prototype.concat().
- Symbol.iterator - A method returning the default iterator for an object. Used by for…of.
- Symbol.match - A method that matches against a string, also used to determine if an object may be used as a regular expression. Used by String.prototype.match().
- Symbol.matchAll - A method that returns an iterator, that yields matches of the regular expression against a string. Used by String.prototype.matchAll().
- Symbol.replace - A method that replaces matched substrings of a string. Used by String.prototype.replace().
- Symbol.search - A method that returns the index within a string that matches the regular expression. Used by String.prototype.search().
- Symbol.split - A method that splits a string at the indices that match a regular expression. Used by String.prototype.split().
- Symbol.species - A constructor function that is used to create derived objects.
- Symbol.toPrimitive - A method converting an object to a primitive value.
- Symbol.toStringTag - A string value used for the default description of an object. Used by Object.prototype.toString().
- Symbol.unscopables - An object value of whose own and inherited property names are excluded from the with environment bindings of the associated object.