Skip to content

Symbol data type in JavaScript

  • by

JavaScript Symbol data type is a primitive data type, introduced in ECMAScript 2015. A symbol is a unique and immutable value that can be used as the key of an object property.

Use the Symbol() function to create a Symbol

const mySymbol = Symbol();

Each time you call Symbol(), a new and unique symbol is created. You can also pass a string to the Symbol() function to create a named symbol:

const myNamedSymbol = Symbol('mySymbolName');

Symbols can be used as keys in object properties, like this:

const myObj = {};
const mySymbol = Symbol('mySymbolName');
myObj[mySymbol] = 'Hello, world!';
console.log(myObj[mySymbol]); // 'Hello, world!'

Symbol data type in JavaScript example

A simple example code adds a Symbol as an Object Key.

<!DOCTYPE html>
<html>
<body>
<script>
    let id = Symbol("id");

    let person = {
        name: "John",
        age: 25,

        // adding symbol as a key
        [id]: 101 // not "id": 123
    };

    console.log(person);
</script>
</body>
</html>

Output:

Symbol data type in JavaScript

There are a number of built-in methods that can be used with Symbol objects.

MethodDescription
for()Searches for existing symbols
keyFor()Returns a shared symbol key from the global symbol registry.
toSource()Returns a string containing the source of the Symbol object
toString()Returns a string containing the description of the Symbol
valueOf()Returns the primitive value of the Symbol object.

Symbols also have a few built-in properties that are accessible using the Symbol global object.

  1. Symbol.iterator: A well-known symbol that represents the default iterator for an object. This symbol is used to define the behavior of the for...of loop and other built-in methods that iterate over objects.
  2. Symbol.toPrimitive: A well-known symbol that is used to define the behavior of an object when it is coerced to a primitive value (e.g., when it is used in an arithmetic operation or converted to a string).
  3. Symbol.toStringTag: A well-known symbol that is used to define the string representation of an object when it is converted to a string using the Object.prototype.toString() method.
  4. Symbol.hasInstance: A well-known symbol that is used to define the behavior of the instanceof operator for a constructor function.
  5. Symbol.species: A well-known symbol that is used to specify the constructor function that should be used to create derived objects (e.g., subclasses) when a built-in method returns a new object.

There are also several other built-in symbols, such as Symbol.isConcatSpreadable, Symbol.match, Symbol.replace, and Symbol.search, which are used to define the behavior of built-in methods for specific types of objects.

Comment if you have any doubts or suggestions on this Js data type topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *