Symbol
A Symbol value is a unique, non-enumerable primitive value used for encapsulation or information hiding. For example, a symbol can be a key of an object that can never collide with any other key.
Overview
A Symbol value is a unique, non-enumerable primitive value used for encapsulation or information hiding. For example, a symbol can be a key of an object that can never collide with any other key.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 38 | 12 | 36 | 9 | 38 | 9 | |
| The Array[Symbol.species] static accessor property returns the constructor used to construct return values from array methods. | 51 | 79 | 48 | 10 | 51 | 10 |
| The [Symbol.toPrimitive]() method of Symbol values returns this symbol value. | 47 | 15 | 44 | 10 | 47 | 10 |
| The Symbol() function returns primitive values of type Symbol. | 38 | 12 | 36 | 9 | 38 | 9 |
| The description accessor property of Symbol values returns a string containing the description of this symbol, or undefined if the symbol has no description. | 70 | 79 | 63 | 12.1 | 70 | 12.2 |
| The Symbol.for() static method searches for existing symbols in a runtime-wide symbol registry with the given key and returns it if found. Otherwise a new symbol gets created in the global symbol registry with this key. | 40 | 12 | 36 | 9 | 40 | 9 |
| The Symbol.hasInstance static data property represents the well-known symbol Symbol.hasInstance. The Operators/instanceof operator looks up this symbol on its right-hand operand for the method used to determine if the constructor object recognizes an object as its instance. | 50 | 15 | 50 | 10 | 50 | 10 |
| The Symbol.isConcatSpreadable static data property represents the well-known symbol Symbol.isConcatSpreadable. The Array.prototype.concat() method looks up this symbol on each object being concatenated to determine if it should be treated as an array-like object and flattened to its array elements. | 48 | 15 | 48 | 10 | 48 | 10 |
| The Symbol.iterator static data property represents the well-known symbol Symbol.iterator. The iterable protocol looks up this symbol for the method that returns the iterator for an object. In order for an object to be iterable, it must have a [Symbol.iterator] key. | 43 | 12 | 36 | 10 | 43 | 10 |
| The Symbol.keyFor() static method retrieves a shared symbol key from the global symbol registry for the given symbol. | 40 | 12 | 36 | 9 | 40 | 9 |
| The Symbol.match static data property represents the well-known symbol Symbol.match. The String.prototype.match() method looks up this symbol on its first argument for the method used to match an input string against the current object. This symbol is also used to determine if an object should be treated as a regex. | 50 | 79 | 40 | 10 | 50 | 10 |
| The Symbol.matchAll static data property represents the well-known symbol Symbol.matchAll. The String.prototype.matchAll() method looks up this symbol on its first argument for the method that returns an iterator, that yields matches of the current object against a string. | 73 | 79 | 67 | 13 | 73 | 13 |
| The Symbol.replace static data property represents the well-known symbol Symbol.replace. The String.prototype.replace() and String.prototype.replaceAll() methods look up this symbol on their first argument for the method that replaces substrings matched by the current object. | 50 | 79 | 49 | 10 | 50 | 10 |
| The Symbol.search static data property represents the well-known symbol Symbol.search. The String.prototype.search() method looks up this symbol on its first argument for the method that returns the index within a string that matches the current object. | 50 | 79 | 49 | 10 | 50 | 10 |
| The Symbol.species static data property represents the well-known symbol Symbol.species. Methods that create copies of an object may look up this symbol on the object for the constructor function to use when creating the copy. | 51 | 13 | 41 | 10 | 51 | 10 |
| The Symbol.split static data property represents the well-known symbol Symbol.split. The String.prototype.split() method looks up this symbol on its first argument for the method that splits a string at the indices that match the current object. | 50 | 79 | 49 | 10 | 50 | 10 |
| The Symbol.toPrimitive static data property represents the well-known symbol Symbol.toPrimitive. All type coercion algorithms look up this symbol on objects for the method that accepts a preferred type and returns a primitive representation of the object, before falling back to using the object's valueOf() and toString() methods. | 47 | 15 | 44 | 10 | 47 | 10 |
| The toString() method of Symbol values returns a string representing this symbol value. | 38 | 12 | 36 | 9 | 38 | 9 |
| The Symbol.toStringTag static data property represents the well-known symbol Symbol.toStringTag. Object.prototype.toString() looks up this symbol on the this value for the property containing a string that represents the type of the object. | 49 | 15 | 51 | 10 | 49 | 10 |
| Due to a WebIDL spec change in mid-2020, browsers are adding a Symbol.toStringTag property to all DOM prototype objects. For example, to access the Symbol.toStringTag property on HTMLButtonElement: | 50 | 79 | 78 | 14 | 50 | 14 |
| The valueOf() method of Symbol values returns this symbol value. | 38 | 12 | 36 | 9 | 38 | 9 |
- Edge 12 included Symbol properties in `JSON.stringify()` output.
- This browser only partially implements this feature
- This feature was removed in a later browser version (12.1)
- No support for an undefined description.
- This browser only partially implements this feature
- This feature was removed in a later browser version (12.2)
- No support for an undefined description.
Syntax
const id = Symbol('id');
const user = { [id]: 123, name: 'Taro' };
user[id]; // 123
Object.keys(user); // ['name'] (Symbols are not enumerated)
// Well-known Symbol
class Range {
constructor(start, end) { this.start = start; this.end = end; }
[Symbol.iterator]() {
let current = this.start;
return { next: () => ({ value: current, done: current++ > this.end }) };
}
} Live demo
Symbol one
sameDescriptiontext has Symbol create also, each that to differentvalue in exists and inspect it..
privateproperty. simulate
Symbol ki- to and, Object.keys etc. Normal. columnraise from is hide..
Iterateprocessing. kastamize
Looppossible to.. with Symbol.iterator implementation,. Object for...of.
Use cases
Using Symbol
A Symbol value is a unique, non-enumerable primitive value used for encapsulation or information hiding. For example, a symbol can be a key of an object that can never collide with any other key.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features