Transcript of the tutorial.
TOP
So the last thing I want to talk about in this chapter, the last modern feature, is this notion now
of symbols in JavaScript.
So in this course so far we've seen all but one of the data types, and that last remaining data type
is called a symbol.
And the main feature of a symbol is that it's completely unique.
No two symbols can ever be equal to each other.
So what I'm going to do is demo this.
I'm going to say const, and I'm going to call this symbol one and set it equal to symbol.
Now, notice we don't have the new keyword here.
We're not creating a new symbol object.
We're just creating a symbol.
And a symbol is a primitive type.
So we're creating that symbol.
I'm going to create another one by saying const symbol two is now equal to a symbol.
And then what I'm going to do is just log one of these to the console.
So I'll say console dot log symbol one.
Okay, Save that and preview and we can see over here symbol.
Now, I'm also going to log the type of the symbol one.
So I'll say type of symbol one and save this.
And we should see that the type is a symbol.
Now, I'm also going to log out here symbol two.
So I'll say symbol two, comma, and save this.
And notice over here, these two right here, they look identical.
But like I said, no two symbols can ever be the same and I can demo that.
I'm also going to log to the console down here.
Console dot log symbol one is triple equals to symbol two.
And we should see false in the console, which we do because no two symbols are the same.
Even if this was loose comparison with two equal signs, this would still be false.
They are completely unique.
Now we can also pass in identifiers to the symbols to describe what they are.
So I could say something like a generic name here and I could even do the same one here, a generic
name.
And these, even though they're identical here, they would still not be equal.
So I could save this and we would still get false even though they look identical.
So what does this all mean then?
Well, symbols can be used as keys or property names in objects.
For example, we could create a normal object literal now and I'll do that.
I'll say const ninja is equal to a new object.
Then we could just add normal properties by saying something like Ninja dot age is equal to 30.
Or we could use square bracket notation.
So ninja and then in square brackets a property called belt and that would be equal to orange like so.
And if we want to console dot log the ninja now and save this, let's try this.
We can see those properties.
No, we can't because we've misspelled Ninja right there.
So let's save that.
And now we can see those properties.
Now, if I wanted to override one of these properties like the belt, I could say Ninja and then the
belt again and this time it would be equal to black.
Okay, so if I save this now, it overwrites that property so it doesn't give it another belt property.
And we have two belt properties now.
It just overwrites the belt property that we already have.
So we can't give an object this way.
Two properties which are essentially the same now with symbols, that doesn't matter.
These two can look identical, but we can still use them both as properties in an object.
So for example, I could come down here and I could say ninja, and then symbol one is equal to Ryu,
and then down here we'll say ninja and symbol two is this time equal to Sean.
And now if I save this and preview, then we can see that it has both of these properties.
Symbol a generic name Ryu symbol a generic name.
Sean So these look identical, but they're not.
And when we add them as keys to an object, it doesn't matter that they look identical because technically
they are not the same thing because no symbol is the same.
So even though both of the symbols have the same identifier, that doesn't matter.
They're still unique.
So using a symbol as an object key means that all the keys in an object are definitely unique.