Chapter XXIV · Advanced Concepts

Symbols

A Symbol is a primitive data type that produces unique, immutable identifier values guaranteed not to collide with string keys.

01 How It Works

Think of an encrypted cryptographic serial number tag assigned exclusively to a single secure item.

Every Symbol() call creates a completely unique value, making them ideal for creating hidden or private object property keys.

example.js
const id = Symbol("id");
const user = { [id]: 123 };
console.log(user[id]); // 123
  1. 1. Instantiate a symbol using `Symbol('description')`.

  2. 2. Use symbol identifiers as unique object keys.

  3. 3. Prevent property name collision bugs.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
const UNIQUE_KEY = Symbol("key");
const obj = { [UNIQUE_KEY]: "secret value" };

Key Takeaway

Symbols guarantee unique object property keys, preventing accidental naming collisions.

Related Concepts