Javascript - Symbol

About

A symbol is a unique and immutable data type.

New in ES6.

Usage:

  • Identifier for object properties.

Syntax

Symbol([description])

Properties

  • Symbol is not a constructor.
  • Type: Symbol
console.log ( typeof Symbol() === 'symbol' );
  • Description
console.log ( Symbol('myDescription').toString()  );

Example

// Creation of three new symbols. 
// Symbol("foo") does not coerce the string "foo" into a symbol. 
// It creates a new symbol each time
var sym1 = Symbol();
var sym2 = Symbol("foo");
var sym3 = Symbol("foo");

// false
var symFooEqual = (sym2  === sym3)
console.log("sym2 = sym3 ? " + symFooEqual.toString() ); 

Documentation / Reference


Powered by ComboStrap