Sub topic 1
What a Symbol Is: A Unique Primitive
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null, and symbol. The symbol type was added in ES2015. A symbol value is guaranteed to be unique. This chapter explains how to create symbols, what that guarantee means, and how symbols differ from the other primitives.
Prerequisites
This chapter assumes you already know:
- JavaScript's primitive types and how
typeofreports them - How strict equality (
===) compares primitives by value and objects by identity - How to call a function and how the
newoperator works with constructors
Creating a Symbol
You create a symbol by calling the Symbol function. You can pass an optional string, called a description, that labels the symbol for debugging.
const a = Symbol('id');
const b = Symbol('id');
const c = Symbol();
console.log(typeof a); // "symbol"
console.log(typeof c); // "symbol"
console.log(a === b); // false
console.log(a === a); // trueBoth a and b have the description 'id', yet they are not equal. The description does not identify the symbol. It is only a label. Every call to Symbol() produces a value that has never existed before and is equal only to itself.
How Uniqueness Differs from Other Primitives
Most primitives compare by their content. Two strings with the same characters are equal, and two numbers with the same value are equal. Symbols are primitives too, but they behave more like object identity: a symbol is equal only to the exact value that one particular Symbol() call returned.
| Expression | Result | Why |
|---|---|---|
| `'id' === 'id'` | `true` | Strings compare by content |
| `42 === 42` | `true` | Numbers compare by value |
| `Symbol('id') === Symbol('id')` | `false` | Each symbol is a new, unique value |
| `{} === {}` | `false` | Objects compare by identity |
Symbols are still primitives. They have no own properties you can add, they are immutable, and typeof reports "symbol", not "object". You can store them in variables, pass them to functions, and put them in collections like any other value.
const red = Symbol('red');
const green = Symbol('green');
const seen = new Set([red, green, red]);
console.log(seen.size); // 2
console.log(seen.has(red)); // true
console.log(seen.has(Symbol('red'))); // falseSymbol Is Not a Constructor
Symbol looks like a built-in class such as String or Number, but you cannot call it with new.
try {
new Symbol('id');
} catch (e) {
console.log(e instanceof TypeError); // true
console.log(e.message); // e.g. "Symbol is not a constructor"
}This restriction is deliberate. With new String('x') you get a wrapper object, which is a common source of bugs because typeof returns "object" and two wrappers are never equal. The language blocks new Symbol() so that you cannot create a symbol wrapper by accident.
Common Errors
- Expecting the description to identify the symbol. Calling
Symbol('id')twice gives two unrelated symbols. To use the same symbol in two places, keep a reference to one value and pass it around, or use the global registry covered later in this tutorial. - Using
new Symbol(). It throws aTypeError. CallSymbol()directly. - Re-creating a symbol in order to compare it.
x === Symbol('id')is alwaysfalse. Compare against the stored variable.
Exercise
What does this code log, and why? const k1 = Symbol('key'); const k2 = k1; const k3 = Symbol('key'); console.log(k1 === k2, k1 === k3);
Show answer
It logs true false. k2 holds the same symbol value as k1 because assignment copies the value. k3 comes from a separate Symbol() call, so it is a new, unique symbol, even though its description is the same.
Exercise
A teammate writes const tag = new Symbol('tag'); and gets an error. What is the error, and how should the line be fixed?
Show answer
The line throws a TypeError because Symbol cannot be used as a constructor. Fix it with const tag = Symbol('tag');.