What are symbols and how they can be useful to you

JavaScript

The symbol is a new primitive value introduced by ES6. It aims to provide us with unique identifiers. In this article, we explore how it works, in what way it is used in the JavaScript language and how we can benefit from that.

Creating symbols

To create a symbol, we use its constructor.

The function has an optional string parameter that acts as a description.

Symbol(description)

The significant thing is that even if you use the same description twice, every symbol is unique.

false

The symbol is a new primitive value and has its own type. We can check it using typeof:

symbol

Converting the symbol type

In the “Looking into assembly code of coercion” article, we explore how we can convert types in Javascript. A big part of it is the implicit conversion that happens when we use values of different types together:

1 added to 2 equals 3

Even though other types often converse under the hood, it does not include the symbol type.

Uncaught TypeError: Cannot convert a Symbol value to a string

If you would want to use symbols in such a way, converse them explicitly or use the   property.

Hello world!

How to use symbols

Before the introduction of symbols, the object keys could only be strings. Trying to use an object as the key for a property does not give us an expected result.

The things are different with symbols. The ECMAScript specification states that we can use them as keys:

property

part of an object that associates a key (either a String value or a Symbol value) and a value

Let’s try to do it!

Even if two symbols have the same description, they still don’t collide with each other when we use them as keys:

false

It means that we can assign an unlimited number of unique symbols and don’t worry about them evert conflicting with each other.

Accessing the value

Now, the only way to access our value is to use the symbol.

There are certain differences when it comes to iterating through the properties of an object with symbols. The  , and   functions don’t give us access to any values that use symbols – the same goes for a  loop. The way to iterate through them is to use the   function.

Hello world!

Judging by the above, we can conclude that symbols provide some hidden layer under in the object, separate from keys that are strings.

Symbols are unique… most of the time

The way to create a global, not-unique symbol is to use the   function. You need to provide it with a string – it searches the symbol registry for a symbol associated with the given key and returns it if found. If that’s not the case, it creates a new symbol.

true

If you have doubts about a symbol being unique, you can use the   function. It returns the associated key if found.

key

undefined

Well-known symbols

With the introduction of symbols through the ES6, the language itself incorporates it into some of its mechanisms.

Symbol.toStringTag

With the   we can change the internal   property of an object that is used when stringifying an object.

If you would like to know more, look into the Looking into assembly code of coercion for additional information

Symbol.iterator

The   loop makes use of the symbols under the hood. Wit the use of the  the values to iterate over.

a
b
c

Knowing this, you can override the default value that   holds to change the behavior of the   loop.

0.0523720769432241
0.9489775095218018
0.9041067477874434

The above example uses generators. If you would like to know more about them, check out Demystifying generators. Implementing async/await

Other internal symbols

There are many different symbols that you can use to alter the mechanisms of the language. If you are interested in a complete list, check out the MDN documentation.

Summary

In this article, we’ve covered the Symbol type. There are quite a few ways to use that knowledge: by exploring how symbols work we’ve learned how to modify the JavaScript mechanisms through some of the well-known symbols. By using symbols, we can prevent values from colliding with each other, for example in a way that it happened with the MooTools library: if you need to modify the built-in prototypes like the String and Array, it might be a good idea to use Symbols for that. Since symbols are quite an interesting part of the JavaScript language, it is worth trying out.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments