Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Symbol — unique values and property keys

Symbol() creates a unique, immutable primitive value that can serve as an object property key. A description does not determine a symbol's identity; Symbol.for() instead allows a symbol to be shared through a registry.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

Symbol()
Symbol(description)
Symbol.for(key)
Symbol.keyFor(symbol)

Symbol(description) returns a new, unique primitive value of type symbol, not an object. The optional description helps identify the symbol, but is not its identity: two calls to Symbol("id") produce different values. Calling new Symbol() throws TypeError.

A symbol can be a property key independent of string keys. The syntax { [first]: "internal" } creates a property keyed by the value stored in first, and record[first] reads its value. The property record.id has a different key and can coexist with it without a collision.

Symbol.for(key) looks up or creates a symbol in the global registry: the same string key returns the same symbol. Symbol.keyFor(symbol) returns its registry key, or undefined if the symbol is not registered. This differs from creating a new symbol with Symbol().

A symbol key does not provide privacy: it can be discovered through Object.getOwnPropertySymbols(), although Object.keys() omits it. Built-in symbols such as Symbol.iterator are a separate group of predefined keys used by language protocols.

Examples

const first = Symbol("id");
const second = Symbol("id");
const record = { id: "public", [first]: "internal" };

console.log(typeof first);
console.log(first === second);
console.log(record.id);
console.log(record[first]);
console.log(Symbol.for("app.id") === Symbol.for("app.id"));
console.log(Symbol.keyFor(Symbol.for("app.id")));

Result

symbol
false
public
internal
true
app.id

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources