JavaScript: Symbol Tutorial

By Xah Lee. Date: . Last updated: .

New in JS2015.

  1. Symbol is a primitive type. [see Primitive Value]
  2. Symbol is one of the allowed type for the key of a object property. (the other is string)
  3. The purpose of symbol is to avoid property name collision.
  4. Each symbol value is unique and immutable.
  5. Each symbol value has an associated value called “Description”, usually denoted in document as [[Description]]. Its value is either undefined or a string. This [[Description]] cannot be changed once created.

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

const x = Symbol();
const y = Symbol();
const z = Symbol("my cat");

console.log(x); // prints Symbol()
console.log(y); // prints Symbol()
console.log(z); // 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".

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

[see Value Types]

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.

You can add properties to objects in 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:

// adding a symbol key property
const x = Symbol();
const obj = {};
obj[x] = 3;
console.log( obj[x] === 3 ); // true
// adding a symbol key property, via symbol value directly
const obj = {};
obj[Symbol()] = 4;

console.log(
 obj[Object.getOwnPropertySymbols(obj)[0]] === 4
); // true

Note: you cannot use dot notation for property keys that are symbol. [see Property Dot Notation / Bracket Notation]

Symbol Property in Object Literal Expression

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

const obj = {[expression]:value}

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

console.log( obj ); // { [Symbol()]: 3 };

[see ES2015 Object Literal Expression Extensions]

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 obj = {[Symbol()]:4};

console.log( 
 obj[(Object.getOwnPropertySymbols ( obj ))[0]] === 4
); // true
// exists

// symbol key properties are ignored by many constructs
console.log(Object.keys(obj)); // []
console.log(Object.getOwnPropertyNames(obj)); // []
for (let p in obj) { console.log("yes"); } // prints nothing

Check Symbol Property Existence

obj.hasOwnProperty(key)

[see Object.prototype.hasOwnProperty]

To check if a property key is in a object obj's prototype chain (including if it's its own property), use one of:

[see 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()

const ss = Symbol("cat");
console.log( ss.toString() === "Symbol(cat)"); // true
console.log( "my " + ss.toString() === "my Symbol(cat)"); // true

// console.log( "my " + ss);
// TypeError: Cannot convert a Symbol value to a string

JavaScript Property

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc