JavaScript 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
const Array1 = [1, 2, 3];
const Array2 = [4, 5, 6];
let Array3 = Array1.concat(Array2);
console.log(Array3);
Array2[Symbol.isConcatSpreadable] = false ;
Array3 = Array1.concat(Array2);
console.log(Array3);
|
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 Constructor: In JavaScript, a constructor gets called when an object is created.
Constructor |
Description |
Example |
Symbol() |
Used to create a symbol object |
|
JavaScript Symbol Properties: A JavaScript property is a member of an object that associates a key with a value.
- Static Property: A static property is a property that has the same value for the entire class.
Properties
|
Description
|
Example
|
asyncIterator |
It sets an object as async iterable |
|
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. |
|
iterator |
It makes an element easier to use in for..of loop |
|
match |
It identifies matching of a Regular Expression to a string |
|
matchAll |
It returns Regular Expression that matches the String |
|
replace |
Replace the matched substring of a string. |
|
search |
Returns the index within a string that matches the regular expression. |
|
species |
It is used to create a derived object from a function-valued property |
|
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. |
|
- Instance Properties: An instance property is a property that has a new copy for every new instance of the class.
Properties
|
Description
|
Example
|
constructor |
Return the string constructor function for the object. |
|
description |
Return the optional description of the specified symbol objects. |
|
JavaScript Symbol Methods: JavaScript methods are actions that can be performed on objects. There are two types of Symbol methods in JavaScript.
- Static Method: If the method is called using the symbol class itself then it is called a static method.
Methods
|
Description
|
Example
|
for() |
The Symbol.for() is used to search for the given symbol |
|
keyFor() |
This key is retrieved from the global symbol registry. |
|
- Instance Method: If the method is called on an instance of a Symbol then it is called an instance method.
Methods |
Description |
Example |
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. |
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 May, 2023
Like Article
Save Article