JavaScript: Symbol Tutorial

By Xah Lee. Date: . Last updated: .

New in JS2015.

What is Symbol

Create Symbol

Symbol()
Return a 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. Note: 2 symbols with same description are still different symbols.

// creating symbol

let xx = Symbol();
let yy = Symbol();
let zz = Symbol("my cat");

console.log(xx); // prints Symbol()
console.log(yy); // prints Symbol()
console.log(zz); // prints Symbol(my cat)

[see const Declaration]

「Symbol()」 Cannot be Used with 「new」

Symbol() cannot be used with new.

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

Symbol() Return a Unique Symbol, Always

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

typeof Symbol

typeof 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 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.

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.

console.log(obj) may or may not print symbol properties of obj.

[see List Properties]

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

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

// 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

Check Property Existence

List Symbol Keys

Convert Symbol to String

To convert symbol to string, do

sym.toString()

Note: "some " + symbol results TypeError. you must use "some " + symbol.toString()

// convert a symbol value to string

const ss = Symbol("xx");

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

JavaScript Symbol

BUY
ΣJS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc