Skip to content

Creating Symbols

Symbols are created using the Symbol() function. Each call creates a completely unique value.

const sym1 = Symbol('debug');
const sym2 = Symbol('debug');
console.log(sym1 === sym2); // false — every Symbol is unique!
console.log(typeof sym1); // 'symbol'
console.log(sym1.toString()); // 'Symbol(debug)'
const sym1 = Symbol.for('app.id');
const sym2 = Symbol.for('app.id');
console.log(sym1 === sym2); // true — same symbol from registry
console.log(Symbol.keyFor(sym1)); // 'app.id'
AspectSymbol()Symbol.for()
UniquenessAlways uniqueShared globally
Same descriptionDifferent symbolsSame symbol
Use casePrivate, unique keysCross-realm sharing
const id = Symbol('id');
const user = {
name: 'Alice',
[id]: 12345 // Symbol as property key
};
console.log(user[id]); // 12345
console.log(user.name); // 'Alice'
// Symbols don't appear in normal enumeration
console.log(Object.keys(user)); // ['name']
console.log(JSON.stringify(user)); // {'name':'Alice'}
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(id)]
  • Symbol() creates unique, private keys
  • Symbol.for() creates/retrieves global symbols
  • Symbols as property keys are hidden from normal iteration
  • Object.getOwnPropertySymbols() retrieves symbol keys
  • Great for metadata, private properties, and framework internals