What are JavaScript Symbols advanced usage?
Answer
Symbols' deeper applications extend far beyond simple unique property keys. Well-known Symbols are hooks into JavaScript internals: Symbol.iterator — makes objects iterable (for...of); Symbol.asyncIterator — async iteration (for await...of); Symbol.toPrimitive — custom type conversion; Symbol.toStringTag — custom Object.prototype.toString result; Symbol.hasInstance — customize instanceof: class EvenNumber { static [Symbol.hasInstance](n) { return typeof n === "number" && n % 2 === 0; } }; 4 instanceof EvenNumber // true; Symbol.species — specify the constructor for derived objects (used by map, filter, etc.); Symbol.isConcatSpreadable — control Array.prototype.concat behavior; Symbol.match, Symbol.replace, Symbol.search, Symbol.split — customize string methods. Global symbol registry: Symbol.for("key") returns the same symbol for the same key across realms (iframes, workers) — unlike regular symbols which are always unique. Symbol.keyFor(sym) retrieves the key for a registered symbol.
Previous
What is JavaScript's type system and coercion in advanced detail?
Next
What is the difference between inheritance patterns in JavaScript?