Symbol() constructor

The Symbol() constructor returns a value of type symbol, but is incomplete as a constructor because it does not support the syntax "new Symbol()" and it is not intended to be subclassed. It may be used as the value of an extends clause of a class definition but a super call to it will cause an exception.

Syntax

Symbol([description])

Parameters

description Optional
A string. A description of the symbol which can be used for debugging but not to access the symbol itself.

Examples

Creating symbols

To create a new primitive symbol, you write Symbol() with an optional string as its description:

let sym1 = Symbol()
let sym2 = Symbol('foo')
let sym3 = Symbol('foo')

The above code creates three new symbols. Note that Symbol("foo") does not coerce the string "foo" into a symbol. It creates a new symbol each time:

Symbol('foo') === Symbol('foo')  // false

new Symbol(...)

The following syntax with the new operator will throw a TypeError:

let sym = new Symbol()  // TypeError

This prevents authors from creating an explicit Symbol wrapper object instead of a new symbol value and might be surprising as creating explicit wrapper objects around primitive data types is generally possible (for example, new Boolean, new String and new Number).

If you really want to create a Symbol wrapper object, you can use the Object() function:

let sym    = Symbol('foo');
let symObj = Object(sym);
typeof sym    // => "symbol"
typeof symObj // => "object"

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Symbol constructor' in that specification.

Browser compatibility

DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Symbol() constructorChrome Full support 38Edge Full support 12Firefox Full support 36IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 36Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12

Legend

Full support
Full support
No support
No support

See also