JavaScript Symbol Complete Reference
The symbol is a data type of primitive data type. The Symbol() function returns the symbol value type. The value returned from Symbol() is unique.
Syntax:
[Symbol.peoperty](string)
Example: JavaScript code to show the working of this function.
Javascript
<script> // Creating some arrays const Array1 = [1, 2, 3]; const Array2 = [4, 5, 6]; // Calling concat() function let Array3 = Array1.concat(Array2); // Printing the concatenated array console.log(Array3); // Calling Symbol.isConcatSpreadable symbol Array2[Symbol.isConcatSpreadable] = false ; Array3 = Array1.concat(Array2); // Printing the concatenated array // after calling of Symbol.isConcatSpreadable symbol console.log(Array3); </script> |
Output:
Array [1, 2, 3, 4, 5, 6] Array [1, 2, 3, Array [4, 5, 6]]
The complete list of JavaScript Symbol is listed below:
JavaScript Symbol Static Properties:
Properties | Description |
---|---|
hasInstance | Determine if a given constructor object recognizes the object as its instance. |
isConcatSpreadable | Given object should be flattened to its array elements while using the Array.prototype.concat() method. |
JavaScript Symbol.match | Regular expression against a string and this function is called using |
replace | Replace the matched substring of a string. |
search | Returns the index within a string that matches the regular expression. |
split | Specify the method that splits a string at the indices that match a regular expression. |
toStringTag | Creation of the default string description of an object. |
unscopables | Specify an object value of whose own and inherited property names are excluded from the environment bindings. |
JavaScript Static methods
Methods | Description |
---|---|
for() | The Symbol.for() is used to search for the given symbol |
keyFor() | This key is retrieved from the global symbol registry. |
JavaScript Symbol Instance Properties:
Properties | Description |
---|---|
description | Return the optional description of the specified symbol objects. |
JavaScript Symbol Instance Methods:
Methods | Description |
---|---|
toString() | convert the specified symbol object into the string. |
valueOf() | Return the primitive value of a given symbol object. |
@@toPrimitive() | Convert a given symbol object to a primitive value. |
Please Login to comment...