Symbol Type
In this page:
Creating Symbols
Calling Symbol() creates a unique symbol value — every single call produces a genuinely distinct symbol, even if you pass the exact same description string to two different calls.
Example: Creating Symbols
const a = Symbol("id");
const b = Symbol("id");
console.log(a === b);
Symbols as Object Keys
A symbol can be used as a computed property key on an object, and that property can only be accessed again using the exact same symbol reference — no other symbol or string will reach it.
Example: Symbols as Object Keys
const idKey = Symbol("id");
const user = { name: "Ravi", [idKey]: 42 };
console.log(user[idKey]);
Symbol Properties and Enumeration
Symbol-keyed properties are deliberately invisible to Object.keys and normal for...in enumeration, though Object.getOwnPropertySymbols can retrieve an object's symbol keys if you specifically go looking for them.
Example: Symbol Properties and Enumeration
const idKey = Symbol("id");
const user = { name: "Ravi", [idKey]: 42 };
console.log(Object.keys(user));
console.log(Object.getOwnPropertySymbols(user));
Symbol Registry
Symbol.for creates or retrieves a symbol from a global, shared registry keyed by string — unlike Symbol(), two calls to Symbol.for with the same key return the exact same symbol.
Example: Symbol Registry
const a = Symbol.for("shared");
const b = Symbol.for("shared");
console.log(a === b);
Symbols in APIs
Symbols are how JavaScript defines special object protocols internally (like Symbol.iterator) and let independent pieces of code add properties to shared objects without any risk of colliding property names.
Example: Symbols in APIs
class Range {
constructor(private start: number, private end: number) {}
[Symbol.iterator]() {
let current = this.start;
const end = this.end;
return {
next: () => current < end
? { value: current++, done: false }
: { value: undefined, done: true },
};
}
}
console.log([...new Range(1, 4)]);
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: