The data type symbol is a primitive data type. 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 primary purpose, although other use-cases exist, such as enabling opaque data types, or serving as an implementation-supported unique identifier in general. Some further explanation about purpose and usage can be found in the glossary entry for Symbol.
Description
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
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')
typeof sym // "symbol"
let 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.
Finding symbol properties on objects
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.
Constructor
Symbol()- Creates a new
Symbolobject. It is incomplete as a constructor because it does not support the syntax "new Symbol()".
Static properties
Symbol.asyncIterator- A method that returns the default AsyncIterator for an object. Used by
for await...of. Symbol.hasInstance- A method determining if a constructor object recognizes an object as its instance. Used by
instanceof. Symbol.isConcatSpreadable- A Boolean value indicating if an object should be flattened to its array elements. Used by
Array.prototype.concat(). Symbol.iterator- A method returning the default iterator for an object. Used by
for...of. Symbol.match- A method that matches against a string, also used to determine if an object may be used as a regular expression. Used by
String.prototype.match(). Symbol.matchAll- A method that returns an iterator, that yields matches of the regular expression against a string. Used by
String.prototype.matchAll(). Symbol.replace- A method that replaces matched substrings of a string. Used by
String.prototype.replace(). Symbol.search- A method that returns the index within a string that matches the regular expression. Used by
String.prototype.search(). Symbol.split- A method that splits a string at the indices that match a regular expression. Used by
String.prototype.split(). Symbol.species- A constructor function that is used to create derived objects.
Symbol.toPrimitive- A method converting an object to a primitive value.
Symbol.toStringTag- A string value used for the default description of an object. Used by
Object.prototype.toString(). Symbol.unscopables- An object value of whose own and inherited property names are excluded from the
withenvironment bindings of the associated object.
Static methods
Symbol.for(key)- Searches for existing symbols with the given
keyand returns it if found. Otherwise a new symbol gets created in the global symbol registry withkey. Symbol.keyFor(sym)- Retrieves a shared symbol key from the global symbol registry for the given symbol.
Instance properties
Symbol.prototype.description- A read-only string containing the description of the symbol.
Instance methods
Symbol.prototype.toSource()- Returns a string containing the source of the
Symbolobject. Overrides theObject.prototype.toSource()method. Symbol.prototype.toString()- Returns a string containing the description of the Symbol. Overrides the
Object.prototype.toString()method. Symbol.prototype.valueOf()- Returns the primitive value of the
Symbolobject. Overrides theObject.prototype.valueOf()method. Symbol.prototype[@@toPrimitive]- Returns the primitive value of the
Symbolobject.
Examples
Using the typeof operator with symbols
The typeof operator can help you to identify symbols.
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'
Symbol type conversions
Some things to note when working with type conversion of symbols.
- When trying to convert a symbol to a number, a
TypeErrorwill be thrown (e.g.+symorsym | 0). - When using loose equality,
Object(sym) == symreturnstrue. Symbol("foo") + "bar"throws aTypeError(can't convert symbol to string). This prevents you from silently creating a new string property name from a symbol, for example.- The "safer"
String(sym)conversion works like a call toSymbol.prototype.toString()with symbols, but note thatnew String(sym)will throw.
Symbols and for...in iteration
Symbols 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.
let obj = {}
obj[Symbol('a')] = 'a'
obj[Symbol.for('b')] = 'b'
obj['c'] = 'c'
obj.d = 'd'
for (let i in obj) {
console.log(i) // logs "c" and "d"
}
Symbols and JSON.stringify()
Symbol-keyed properties will be completely ignored when using JSON.stringify():
JSON.stringify({[Symbol('foo')]: 'foo'})
// '{}'
For more details, see JSON.stringify().
Symbol wrapper objects as property keys
When a Symbol wrapper object is used as a property key, this object will be coerced to its wrapped symbol:
let sym = Symbol('foo')
let obj = {[sym]: 1}
obj[sym] // 1
obj[Object(sym)] // still 1
Specifications
| Specification |
| ECMAScript (ECMA-262)The definition of 'Symbol' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Symbol
|
Chrome
Full support 38 |
Edge Full support 12 Full support 12 Notes' Edge 12 included Symbol properties in |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support 25 |
Safari
Full support 9 |
WebView Android
Full support 38 |
Chrome Android
Full support 38 |
Firefox Android
Full support 36 |
Opera Android
Full support 25 |
Safari iOS
Full support 9 |
Samsung Internet Android
Full support 3.0 |
nodejs
Full support 0.12 |
Symbol() constructor
|
Chrome
Full support 38 |
Edge
Full support 12 |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support 25 |
Safari
Full support 9 |
WebView Android
Full support 38 |
Chrome Android
Full support 38 |
Firefox Android
Full support 36 |
Opera Android
Full support 25 |
Safari iOS
Full support 9 |
Samsung Internet Android
Full support 3.0 |
nodejs
Full support 0.12 |
asyncIterator
|
Chrome
Full support 63 |
Edge
Full support 79 |
Firefox
Full support 57 |
IE
No support No |
Opera
Full support 50 |
Safari
Full support 11.1 |
WebView Android
Full support 63 |
Chrome Android
Full support 63 |
Firefox Android
No support No |
Opera Android
Full support 46 |
Safari iOS
No support No |
Samsung Internet Android
Full support 8.0 |
nodejs
Full support 10.0.0 |
description
|
Chrome
Full support 70 |
Edge
Full support 79 |
Firefox
Full support 63 |
IE
No support No |
Opera
Full support 57 |
Safari Full support 12.1 Full support 12.1 Partial support 12 Notes' No support for an undefined description. |
WebView Android
Full support 70 |
Chrome Android
Full support 70 |
Firefox Android
Full support 63 |
Opera Android
Full support 49 |
Safari iOS Full support 12.2 Full support 12.2 Partial support 12 Notes' No support for an undefined description. |
Samsung Internet Android
Full support 10.0 |
nodejs
Full support 11.0.0 |
for
|
Chrome
Full support 40 |
Edge
Full support 12 |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support 27 |
Safari
Full support 9 |
WebView Android
Full support 40 |
Chrome Android
Full support 40 |
Firefox Android
Full support 36 |
Opera Android
Full support 27 |
Safari iOS
Full support 9 |
Samsung Internet Android
Full support 4.0 |
nodejs
Full support 0.12 |
hasInstance
|
Chrome
Full support 50 |
Edge
Full support 15 |
Firefox
Full support 50 |
IE
No support No |
Opera
Full support 37 |
Safari
Full support 10 |
WebView Android
Full support 50 |
Chrome Android
Full support 50 |
Firefox Android
Full support 50 |
Opera Android
Full support 37 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs Full support 6.5.0 Full support 6.5.0 Full support 6.0.0 Disabled' From version 6.0.0: this feature is behind the |
isConcatSpreadable
|
Chrome
Full support 48 |
Edge
Full support 15 |
Firefox
Full support 48 |
IE
No support No |
Opera
Full support 35 |
Safari
Full support 10 |
WebView Android
Full support 48 |
Chrome Android
Full support 48 |
Firefox Android
Full support 48 |
Opera Android
Full support 35 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 6.0.0 |
iterator
|
Chrome
Full support 43 |
Edge
Full support 12 |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support 30 |
Safari
Full support 10 |
WebView Android
Full support 43 |
Chrome Android
Full support 43 |
Firefox Android
Full support 36 |
Opera Android
Full support 30 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 4.0 |
nodejs
Full support 0.12 |
keyFor
|
Chrome
Full support 40 |
Edge
Full support 12 |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support 27 |
Safari
Full support 9 |
WebView Android
Full support 40 |
Chrome Android
Full support 40 |
Firefox Android
Full support 36 |
Opera Android
Full support 27 |
Safari iOS
Full support 9 |
Samsung Internet Android
Full support 4.0 |
nodejs
Full support 0.12 |
match
|
Chrome
Full support 50 |
Edge
Full support 79 |
Firefox
Full support 40 |
IE
No support No |
Opera
Full support 37 |
Safari
Full support 10 |
WebView Android
Full support 50 |
Chrome Android
Full support 50 |
Firefox Android
Full support 40 |
Opera Android
Full support 37 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 6.0.0 |
matchAll
|
Chrome
Full support 73 |
Edge
Full support 79 |
Firefox
Full support 67 |
IE
No support No |
Opera
Full support 60 |
Safari
Full support 13 |
WebView Android
Full support 73 |
Chrome Android
Full support 73 |
Firefox Android
Full support 67 |
Opera Android
Full support 52 |
Safari iOS
Full support 13 |
Samsung Internet Android
No support No |
nodejs
Full support 12.0.0 |
replace
|
Chrome
Full support 50 |
Edge
Full support 79 |
Firefox
Full support 49 |
IE
No support No |
Opera
Full support 37 |
Safari
Full support 10 |
WebView Android
Full support 50 |
Chrome Android
Full support 50 |
Firefox Android
Full support 49 |
Opera Android
Full support 37 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 6.0.0 |
search
|
Chrome
Full support 50 |
Edge
Full support 79 |
Firefox
Full support 49 |
IE
No support No |
Opera
Full support 37 |
Safari
Full support 10 |
WebView Android
Full support 50 |
Chrome Android
Full support 50 |
Firefox Android
Full support 49 |
Opera Android
Full support 37 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 6.0.0 |
species
|
Chrome
Full support 51 |
Edge
Full support 13 |
Firefox
Full support 41 |
IE
No support No |
Opera
Full support 38 |
Safari
Full support 10 |
WebView Android
Full support 51 |
Chrome Android
Full support 51 |
Firefox Android
Full support 41 |
Opera Android
Full support 41 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs Full support 6.5.0 Full support 6.5.0 Full support 6.0.0 Disabled' From version 6.0.0: this feature is behind the |
split
|
Chrome
Full support 50 |
Edge
Full support 79 |
Firefox
Full support 49 |
IE
No support No |
Opera
Full support 37 |
Safari
Full support 10 |
WebView Android
Full support 50 |
Chrome Android
Full support 50 |
Firefox Android
Full support 49 |
Opera Android
Full support 37 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 6.0.0 |
toPrimitive
|
Chrome
Full support 47 |
Edge
Full support 15 |
Firefox
Full support 44 |
IE
No support No |
Opera
Full support 34 |
Safari
Full support 10 |
WebView Android
Full support 47 |
Chrome Android
Full support 47 |
Firefox Android
Full support 44 |
Opera Android
Full support 34 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 6.0.0 |
| Chrome
No support No |
Edge
No support No |
Firefox No support 36 — 74 No support 36 — 74 Notes' Starting in Firefox 74, |
IE
No support No |
Opera
No support No |
Safari
No support No |
WebView Android
No support No |
Chrome Android
No support No |
Firefox Android
Full support 36 |
Opera Android
No support No |
Safari iOS
No support No |
Samsung Internet Android
No support No |
nodejs
No support No | |
toString
|
Chrome
Full support 38 |
Edge
Full support 12 |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support 25 |
Safari
Full support 9 |
WebView Android
Full support 38 |
Chrome Android
Full support 38 |
Firefox Android
Full support 36 |
Opera Android
Full support 25 |
Safari iOS
Full support 9 |
Samsung Internet Android
Full support 3.0 |
nodejs
Full support 0.12 |
toStringTag
|
Chrome
Full support 49 |
Edge
Full support 15 |
Firefox
Full support 51 |
IE
No support No |
Opera
Full support 36 |
Safari
Full support 10 |
WebView Android
Full support 49 |
Chrome Android
Full support 49 |
Firefox Android
Full support 51 |
Opera Android
Full support 36 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs Full support 6.0.0 Full support 6.0.0 Full support 4.0.0 Disabled' From version 4.0.0: this feature is behind the |
unscopables
|
Chrome
Full support 45 |
Edge
Full support 12 |
Firefox
Full support 48 |
IE
No support No |
Opera
Full support 32 |
Safari
Full support 9 |
WebView Android
Full support 45 |
Chrome Android
Full support 45 |
Firefox Android
Full support 48 |
Opera Android
Full support 32 |
Safari iOS
Full support 9 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 0.12 |
valueOf
|
Chrome
Full support 38 |
Edge
Full support 12 |
Firefox
Full support 36 |
IE
No support No |
Opera
Full support 25 |
Safari
Full support 9 |
WebView Android
Full support 38 |
Chrome Android
Full support 38 |
Firefox Android
Full support 36 |
Opera Android
Full support 25 |
Safari iOS
Full support 9 |
Samsung Internet Android
Full support 3.0 |
nodejs
Full support 0.12 |
@@toPrimitive
|
Chrome
Full support 47 |
Edge
Full support 15 |
Firefox
Full support 44 |
IE
No support No |
Opera
Full support 34 |
Safari
Full support 10 |
WebView Android
Full support 47 |
Chrome Android
Full support 47 |
Firefox Android
Full support 44 |
Opera Android
Full support 34 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 6.0.0 |
Legend
- Full support
- Full support
- No support
- No support
- Non-standard. Expect poor cross-browser support.'
- Non-standard. Expect poor cross-browser support.
- See implementation notes.'
- See implementation notes.
- User must explicitly enable this feature.'
- User must explicitly enable this feature.
See also
- Glossary: Symbol data type
typeof- Data types and data structures
- "ES6 In Depth: Symbols" on hacks.mozilla.org
Symbol by Mozilla Contributors is licensed under CC-BY-SA 2.5.