JS: Symbol Tutorial

By Xah Lee. Date: . Last updated: .

New in ES2015.

  1. Symbol is a primitive type. [see JS: Primitive Value]
  2. Symbol is the set of all non-string values that may be used as the key of a object property.
  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 JS: 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 JS: Value Types]

Using Symbol as Property Key

Before ES2015, property key can only be string type. In ES2015, 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 JS: Property Dot Notation / Bracket Notation]

Symbol Property in Object Literal Expression

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

const obj = {[expression]:value}

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

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

[see JS: 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 JS: 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 JS: 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 JS: Check Property Existence]

List Symbol Keys

Object.getOwnPropertySymbols(obj) → return a array of symbol keys of obj.

[see JS: Object.getOwnPropertySymbols]

Reflect.ownKeys(obj) → return a array of all (string and symbol) property keys.

[see JS: Reflect.ownKeys]

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

Symbol Topic

  1. JS: Symbol Tutorial
  2. JS: Symbol Object
  3. JS: Symbol.prototype

JS Object Property

  1. Property Overview
  2. Property Key
  3. Dot vs Bracket Notation
  4. Create/Delete Property
  5. Get/Set Property
  6. Check Property Existence
  7. Access Property
  8. List Properties
  9. Property Attributes
  10. Getter/Setter
  11. Property Descriptor
  12. Symbol
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc