Allows you to create a unique identifier.
Syntax
obj = Symbol(desc)
Parameters
desc
Optional. The description of the symbol.
Remarks
Symbol objects allow properties to be added to existing objects with no possibility of interference with the existing object properties, with no unintended visibility, and with no other uncoordinated additions by other code.
Symbol is a primitive data type. Symbol objects cannot be created using the new operator.
To add symbol objects the global symbol registry, use the Symbol.for and Symbol.keyFor functions. If you serialize symbols as strings, use the global symbol registry to make sure that a particular string maps to the correct symbol for all lookups.
JavaScript includes the following built-in symbol values that are available in the global scope.
| Symbol | Description |
|---|---|
Symbol.hasInstance |
This method determines whether a constructor object recognizes an object as one of the constructor's instances. It is used internally by the instanceof operator. |
Symbol.isConcatSpreadable |
This property returns a Boolean value that indicates whether an object should be flattened to its array elements by Array.concat. |
Symbol.iterator |
This method returns the default iterator for an object. It is used internally by the for...of statement. |
Symbol.toPrimitive |
This method converts an object to a corresponding primitive value. It is used internally by the ToPrimitive abstract operation. |
Symbol.toStringTag |
This property returns a string value that is used to help create the default string description of an object. It is used internally by the built-in method Object.toString method. |
Symbol.unscopables |
This property returns an object whose properties are excluded from the with environment bindings of the associated object. |
Functions
The following table lists the functions of the Symbol object.
| Property | Description |
| Symbol.for | Returns the symbol for a specified key or, if the key is not present, creates a new symbol object with the new key. |
| Symbol.keyFor | Returns the key for a specified symbol. |
Example
(function() {
// module-scoped symbol
var key = Symbol("description");
function MyClass(privateData) {
this[key] = privateData;
}
MyClass.prototype = {
someFunc: function() {
return "data: " + this[key];
}
};
var c = new MyClass("private data")
console.log(key);
console.log(c["key"]);
console.log(c.someFunc());
})();
// Output:
// undefined
// undefined
// data: private data
Requirements
Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.


