JavaScript Symbol Data Type
Symbol (ES6)
Primitive type for unique identifiers.
Symbol Examples
let sym1 = Symbol("description");
let sym2 = Symbol("description"); // Different symbols
console.log(sym1 === sym2); // false
let obj = {
[sym1]: "secret value",
name: "John"
};
console.log(obj[sym1]); // "secret value"
// Global registry
let globalSym = Symbol.for("shared");
let sameSym = Symbol.for("shared"); // Same symbolKey Points
- Always unique, even same description.
- Property keys prevent naming conflicts.
Symbol.iteratorfor custom iteration.Symbol.for()global symbol registry.