Introduction to the Symbol primitive type
ES6 introduced Symbol, a new primitive data type representing a unique value. It is used primarily to ensure that object property names are unique, preventing collisions when adding methods to objects (e.g., in mixin patterns).
Key characteristics:
- Symbols are created using the
Symbol()function. - They are primitive values, not objects; therefore, you cannot add properties to them.
- The
Symbol()function must NOT be called with thenewoperator. - Every Symbol created is guaranteed to be unique, even if they are initialized with the same description.
let s = Symbol();
typeof s; // "symbol"
// Symbols with the same description are NOT equal
let s1 = Symbol('foo');
let s2 = Symbol('foo');
s1 === s2; // false