The Symbol()
function returns a value of type symbol, has static properties that expose several members of built-in objects, has static methods that expose the global symbol registry, and resembles a built-in object class but is incomplete as a constructor because it does not support the syntax "new Symbol()
".
Every symbol value returned from Symbol()
is unique. A symbol value may be used as an identifier for object properties; this is the data type's only purpose. Some further explanation about purpose and usage can be found in the glossary entry for Symbol.
The data type symbol is a primitive data type.
Symbol([description])
description
Optional
To create a new primitive symbol, you write Symbol()
with an optional string as its description:
var sym1 = Symbol(); var sym2 = Symbol('foo'); var 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
The following syntax with the new
operator will throw a TypeError
:
var 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:
var sym = Symbol('foo'); typeof sym; // "symbol" var symObj = Object(sym); typeof symObj; // "object"
The above syntax using the Symbol()
function will not create a global symbol that is available in your whole codebase. To create symbols available across files and even across realms (each of which has its own global scope), use the methods Symbol.for()
and Symbol.keyFor()
to set and retrieve symbols from the global symbol registry.
The method Object.getOwnPropertySymbols()
returns an array of symbols and lets you find symbol properties on a given object. Note that every object is initialized with no own symbol properties, so that this array will be empty unless you've set symbol properties on the object.
Symbol.length
Symbol.prototype
Symbol
constructor.In addition to your own symbols, JavaScript has some built-in symbols which represent internal language behaviors which were not exposed to developers in ECMAScript 5 and before. These symbols can be accessed using the following properties:
Symbol.iterator
for...of
.Symbol.asyncIterator
for await of
.Symbol.match
String.prototype.match()
.Symbol.replace
String.prototype.replace()
.Symbol.search
String.prototype.search()
.Symbol.split
String.prototype.split()
.Symbol.hasInstance
instanceof
.Symbol.isConcatSpreadable
Array.prototype.concat()
.Symbol.unscopables
with
environment bindings of the associated object.Symbol.species
Symbol.toPrimitive
Symbol.toStringTag
Object.prototype.toString()
.Symbol.for(key)
Symbol.keyFor(sym)
Symbol
prototypeAll Symbols inherit from Symbol.prototype
.
Symbol.prototype.constructor
Symbol
function by default.Symbol.prototype.toSource()
Symbol
object. Overrides the Object.prototype.toSource()
method.Symbol.prototype.toString()
Object.prototype.toString()
method.Symbol.prototype.valueOf()
Symbol
object. Overrides the Object.prototype.valueOf()
method.Symbol.prototype[@@toPrimitive]
Symbol
object.typeof
operator with symbolsThe typeof
operator can help you to identify symbols.
typeof Symbol() === 'symbol' typeof Symbol('foo') === 'symbol' typeof Symbol.iterator === 'symbol'
Some things to note when working with type conversion of symbols.
TypeError
will be thrown+sym
or sym | 0
).Object(sym) == sym
returns true.
Symbol("foo") + "bar"
throws a TypeError
(can't convert symbol to string). This prevents you from silently creating a new string property name from a symbol, for example.String(sym)
conversion works like a call to Symbol.prototype.toString()
with symbols, but note that new String(sym)
will throw.for...in
iterationSymbols are not enumerable in for...in
iterations. In addition, Object.getOwnPropertyNames()
will not return symbol object properties, however, you can use Object.getOwnPropertySymbols()
to get these.
var obj = {}; obj[Symbol('a')] = 'a'; obj[Symbol.for('b')] = 'b'; obj['c'] = 'c'; obj.d = 'd'; for (var i in obj) { console.log(i); // logs "c" and "d" }
JSON.stringify()
Symbol-keyed properties will be completely ignored when using JSON.stringify()
:
JSON.stringify({[Symbol('foo')]: 'foo'}); // '{}'
For more details, see JSON.stringify()
.
When a Symbol wrapper object is used as a property key, this object will be coerced to its wrapped symbol:
var sym = Symbol('foo'); var obj = {[sym]: 1}; obj[sym]; // 1 obj[Object(sym)]; // still 1
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Symbol' in that specification. | Standard | Initial definition |
ECMAScript Latest Draft (ECMA-262) The definition of 'Symbol' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 38 | 121 | 36 | No | 25 | 9 |
asyncIterator |
No | No | No2 | No | No | No |
for |
40 | Yes | 36 | No | Yes | 9 |
hasInstance |
51 | Yes | 50 | No | Yes | Yes |
isConcatSpreadable |
48 | Yes | 48 | No | Yes | Yes |
iterator |
43 | Yes | 36 | No | 30 | 10 |
keyFor |
40 | Yes | 36 | No | Yes | 9 |
match |
50 | Yes | 40 | No | Yes | Yes |
prototype |
38 | 12 | 36 | No | 25 | 9 |
replace |
50 | Yes | 49 | No | Yes | Yes |
search |
50 | Yes | 49 | No | Yes | Yes |
species |
51 | 14 | 41 | No | 38 | 10 |
split |
50 | Yes | 49 | No | Yes | Yes |
toPrimitive |
48 | Yes | 44 | No | Yes | Yes |
toSource |
No | No | 36 | No | No | No |
toString |
38 | 12 | 36 | No | 25 | 9 |
toStringTag |
49 | Yes | 51 | No | Yes | Yes |
unscopables |
38 | Yes | 48 | No | Yes | 9 |
valueOf |
38 | 12 | 36 | No | 25 | 9 |
@@toPrimitive |
? | ? | 44 | No | ? | ? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | 38 | Yes | 36 | 25 | 9 | ? |
asyncIterator |
No | No | No | No | No | No | ? |
for |
Yes | Yes | Yes | 36 | Yes | 9 | ? |
hasInstance |
Yes | Yes | Yes | 50 | Yes | Yes | ? |
isConcatSpreadable |
Yes | Yes | Yes | 48 | Yes | Yes | ? |
iterator |
Yes | Yes | Yes | 36 | Yes | 10 | ? |
keyFor |
Yes | Yes | Yes | 36 | Yes | 9 | ? |
match |
Yes | Yes | Yes | 40 | Yes | Yes | ? |
prototype |
Yes | 38 | Yes | 36 | 25 | 9 | ? |
replace |
Yes | Yes | Yes | 49 | Yes | Yes | ? |
search |
Yes | Yes | Yes | 49 | Yes | Yes | ? |
species |
Yes | Yes | 14 | 41 | 38 | 10 | ? |
split |
Yes | Yes | Yes | 49 | Yes | Yes | ? |
toPrimitive |
Yes | Yes | Yes | 44 | Yes | Yes | ? |
toSource |
No | No | No | 36 | No | No | ? |
toString |
Yes | 38 | Yes | 36 | 25 | 9 | ? |
toStringTag |
Yes | Yes | Yes | 51 | Yes | Yes | ? |
unscopables |
Yes | Yes | Yes | 48 | Yes | 9 | ? |
valueOf |
Yes | 38 | Yes | 36 | 25 | 9 | ? |
@@toPrimitive |
? | ? | ? | 44 | ? | ? | ? |
1. Edge 12 included Symbol properties in JSON.stringify()
output.
2. Available in Firefox Nightly.
typeof
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol