21. Introduction to the Symbol Type
Symbol is a primitive data type introduced in ES6. Its primary purpose is to create truly unique identifiers for object properties, preventing naming collisions.
Creating Symbols
Symbols are created using the Symbol() function. They are guaranteed to be unique.
javascript const ID1 = Symbol('id'); const ID2 = Symbol('id');
console.log(ID1 === ID2); // Output: false (Even though they have the same description, they are unique)
let user = { name: 'Jane', [ID1]: 101 // Using Symbol as a property key };
console.log(user[ID1]); // Output: 101
Practical Use Case
When working with complex objects or third-party code, Symbols ensure that the property keys you add won't clash with existing or future keys, making objects more robust and extensible.