The Symbol is a new primitive type
introduced in ES6. It’s introduced in ES6. Symbols are completely unique identifiers. Just like their
primitive counterparts (Number, String, Boolean), they can be created using the
factory function Symbol () which
returns a Symbol.
List of Symbol Features:-
1. The Symbol is used to create a unique identifier
and every symbol value returned from Symbol() is unique.
2. The symbol does not auto convert to strings.
3. Symbols are great to be used as the key in
objects.
4. Two Symbols with the same description is never
equal.
5. The data type symbol is a primitive data type.
6. While iterating over Objects, Symbol as the
key won't be accessible. They become hidden properties. But,
Object.assign({}) , Symbol will also get copied.
Syntax: - Symbol([description])
A description of the symbol which can
be used for debugging but not to access the symbol itself
Example,
const symbol1 = Symbol();
const symbol2 = Symbol(35);
const symbol3 = Symbol('Anil');
console.log(typeof symbol1);
//
expected output is : "symbol"
console.log(symbol3.toString());
//
expected output is : "Symbol(Anil)"
console.log(Symbol('Anil') === Symbol('Anil'));
// expected output is :
false
Do we really need symbols?
1. Enum
2. Name Clashes
3. Privacy
4. Protocols
5. And so on.