JavaScript: Symbol Tutorial

By Xah Lee. Date: . Last updated: .

New in JS2015.

What is Symbol

Create Symbol

Symbol()
Return a symbol.
// create symbol
const xx = Symbol();
console.log(xx);
// prints Symbol()
Symbol(description)
Return a symbol with description string description.
  • The description is a printed representation of the symbol, mostly for debugging purposes.
  • The description is used when you convert symbol to string.

πŸ›‘ WARNING: 2 symbols with same description are still different symbols.

// create symbol
const zz = Symbol("my cat");
console.log(zz);
// prints Symbol(my cat)

The β€œSymbol” Function Cannot be Used with the operator β€œnew”

Symbol() cannot be used with new.

// new Symbol();
// TypeError: Symbol is not a constructor

The β€œSymbol” Function Return a Unique Symbol, Always

// Symbol() always returns a unique symbol
console.log((Symbol() === Symbol()) === false);

typeof Symbol

typeof Operator on symbol value returns string "symbol". [see Value Types]

console.log(typeof Symbol() === "symbol");
console.log(typeof Symbol("abc") === "symbol");

Using Symbol as Property Key

Before JS2015, property key can only be string type. In JS2015, property key can be either a string type or symbol type.

The symbol value is designed to be keys for properties. Because user-created symbol values are unique, this means property key collision cannot happen. This lets you add properties to any object of any libraries and don't have to worry about property key collision.

πŸ’‘ TIP: you normally should assign a symbol value to a variable, otherwise you cannot access the symbol, unless you use numerical index that relies on the order of properties.

Use Bracket Notation to Access Symbol Property

Use bracket notation to access property symbol. Like this:

// add a symbol key property
const ss = Symbol();
const jj = {};
jj[ss] = 3;
console.log(jj[ss] === 3);
// add a symbol key property, via symbol value directly
const jj = {};
jj[Symbol()] = 4;

console.log(jj[Object.getOwnPropertySymbols(jj)[0]] === 4);

Symbol Property in Object Literal Expression

You can use symbol property key in object literal expression (New in JS2015.), like this:

const obj = {[symbol]:value}

[see ES2015 Object Literal Expression Extensions]

// use of symbol key in object literal expression
const jj = { [Symbol()]: 3 };

console.log(jj[Object.getOwnPropertySymbols(jj)[0]] === 3);

Symbol Properties Are Usually Ignored

Symbol properties are usually ignored by constructs that access properties.

[see List Properties]

const ss = Symbol();
const jj = { [ss]: 4 };

// exists
console.log(jj.hasOwnProperty(ss));

// symbol key properties are ignored by many constructs
console.log(Object.keys(jj).length === 0);
console.log(Object.getOwnPropertyNames(jj).length === 0);

Check Symbol Property Existence

List Symbol Keys

Convert Symbol to String

To convert symbol to string, do

sym.toString()

πŸ’‘ TIP: "some " + symbol results TypeError. you must use "some " + symbol.toString()

πŸ›‘ WARNING: symbols without description all become "Symbol()". You cannot distinguish them.

// convert a symbol value to string

const ss = Symbol("xx");

console.log(ss.toString() === "Symbol(xx)");

JavaScript Symbol

BUY Ξ£JS JavaScript in Depth