Symbols and Well-Known Symbols
Symbols provide unique primitive values that can be used as property keys. Two separately created symbols are different even if they have the same description. Well-known symbols such as Symbol.iterator allow JavaScript objects to participate in language protocols.
Symbols are useful when you need non-string property keys or want to integrate with built-in language behavior.
Unique Symbol Keys
const id = Symbol('id');
const user = {
name: 'Suriya',
[id]: 123
};
console.log(user[id]);💡 The symbol becomes a unique property key. Bracket notation is required to access a computed symbol key.
Key Takeaways
- Symbols are unique primitive values.
- Symbols can be object property keys.
- Well-known symbols customize language protocols.
Knowledge Check
What is a key property of Symbol values?