Symbol: Unique Identifiers
What you'll learn: How JavaScript's Symbol type creates values that are guaranteed to be unique, solving the problem of accidental property name collisions.
What is a Symbol?
A Symbol is a primitive type that produces a guaranteed unique value every time you create one. Unlike strings or numbers, two symbols are never equal to each other, even if they have the same description.
Think of symbols like fingerprints: even identical twins have different fingerprints. Similarly, every symbol is unique—no two are ever the same.
Why Symbols Exist
Imagine you're adding a property to an object, but you're worried someone else might use the same property name and overwrite your data. Symbols solve this problem by ensuring your property key is truly one-of-a-kind.
Creating Symbols
You create a symbol using Symbol():
let id1 = Symbol();
let id2 = Symbol();
console.log(id1 === id2); // false — they're different!
Even with descriptions, they remain unique:
let userId = Symbol("user id");
let otherId = Symbol("user id");
console.log(userId === otherId); // false — still different
The description (like "user id") is just a label for debugging—it doesn't affect uniqueness.
Symbols as Property Keys
Symbols are primarily used as object property keys that won't accidentally conflict with other properties:
let uniqueKey = Symbol("my secret key");
let obj = {};
obj[uniqueKey] = "hidden value";
console.log(obj[uniqueKey]); // "hidden value"
Key Takeaway: Symbols are primitive values that are always unique, making them perfect for creating property keys that won't collide with any other property names in your objects.