Symbols & Well-Known Symbols
Symbols & Well-Known Symbols in Modern JavaScript
Introduced in ES6, Symbol is a primitive data type that represents a unique, immutable identifier. Symbols are used to define private-like object properties that never collide with other keys, and to hook into JavaScript's internal engine mechanics via Well-Known Symbols.
1. Creating and Using Symbols
Every call to Symbol() produces a globally unique value, even with the same description:
Using Symbols as Object Keys (Collision-Proof Properties):
2. The Global Symbol Registry: Symbol.for() & Symbol.keyFor()
When multiple scripts or iframes need to share the same Symbol instance:
3. Well-Known Symbols (Engine Hooks)
Well-Known Symbols are built-in symbols that expose internal engine algorithms:
1. Symbol.toPrimitive: Custom Type Coercion
Customizes how an object converts to a string or number:
2. Symbol.hasInstance: Customizing instanceof
3. Symbol.species: Subclass Derivative Constructor
Allows derived classes (like custom Array subclasses) to dictate what constructor built-in methods (like .map() or .filter()) return.
Practice Quiz
WebSockets for Real-Time Bidirectional Communication
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Object.freeze, Object.seal & Immutability | WebSockets for Real-Time Bidirectional Communication |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.