Check out a free preview of the full Professional JS: Features You Need to Know course

The "Symbol" Lesson is part of the full, Professional JS: Features You Need to Know course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses symbols, a new type introduced in ECMAScript 6 that are unique and immutable primitive values. Symbols are often used to create unique property keys for objects and can be used as identifiers or keys in various scenarios.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Symbol" Lesson

[00:00:00]
>> Maximiliano Firtman: Symbol this is the one of the things that have appear in ESX that it's actually difficult to finally understand what is a symbol, Okay? So a symbol first is a constructor and it's a type in JavaScript, so it's a new type that appear in ECMAScript six, they are unique and immutable primitive values.

[00:00:24]
So they're not objects,they're primitive values. So for example, I cannot put a symbol in a weak set that accepts only objects. They're often used to create unique property keys for objects. For example, I create a new symbol. The symbol has a property key that is kind of a label.

[00:00:45]
It's for kind of debugging purposes only. And then I use that symbol, okay? Like that, and that when we create the symbol, that symbol goes to a global collection of symbols. That you can query with symbol for so I can check okay? If I do have a symbol for description, something important is that if you create another symbol with the same description, it's a different one, not the same one.

[00:01:18]
Everyone is unique okay, so even if they have the same property, so it doesn't matter. And you use that, for example, as a key in an object, or as a key for something else. Sometimes it's being used to send us an argument. When you want something unique and you wanna, it's kind of an ID of the current process.

[00:01:49]
So when the software starts, you create a symbol and then you pass that symbol anywhere. That means if you refresh, you will get the different symbol. So it's a different one, things like that. So it's not completely clear initially, yeah.
>> Student: If I remember correctly I know in like Ruby which also has strings as keys for hashes, objects and then symbols as well.

[00:02:15]
Symbols are often preferred because they're like supposedly faster for lookups, etc.. Do you know if JS is fast enough that that's not a consideration here or is it like-
>> Maximiliano Firtman: Symbols should be faster but I don't think you will pick symbols over strings because of that. Because looking into strings is always slower.

[00:02:39]
Think about the symbol like If it's a binary ID or something like that. But I don't think anyone is thinking about that. So I think it's a big issue okay but yeah, you can use symbols and because it's immutable, no one can change it. So it's immutable, which means that That it's more secure if you want.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now