Hello friends.
Nice to see you back.
In this video we are going to use symbol inside the object.
So I'm going to remove these lines.
And I'm going to create a new symbol late age.
Equal to symbol.
For now.
It's empty.
And then I'm going to create an object.
So I'm going to take a variable name for object.
Late user.
Equal to inside the curly braces.
Here I'm going to create the object.
And we know we need to use value and key to create an object.
Our first object key is name.
And its value is at one.
And our second object is class and it its value is class ten.
And now I want to use this symbol as a key.
In that case first we need to use square braces.
And then we need to type symbol variable.
H.
And we need to provide value.
So I'm going to type 25.
It's a numeric value when we need to use symbol as the key inside the object.
Then we need to use square braces.
And inside the square braces we need to provide symbol variable.
Let's try to print this object inside the console.
So I'm going to type console dot log inside the parentheses our object name user user semicolon.
To end this line.
If I save this file you can see in my console it's print the object for our first video.
It's print name second value it's print class.
But for our third value it's print symbol.
And we do not provide any description to our symbol.
If you want then you can as a symbol value I'm going to type H.
If I save this file this time it print symbol H.
And there is another method to use symbol in our object.
So I'm going to remove it.
And also I remove this comma.
And now I'm going to use this symbol in this object.
So outside the object first I'm going to type object name user.
And inside the square braces I'm going to take symbol variable name age equal to.
And I'm going to assign a value 23.
So this is the another way we can use symbol with object.
So if I save this file you can see in my console it print out proper value.
And as you know if you want to see the exact value of any key, you can just you need to type user dot
class.
If I save this file, you can see it's written the exact value plus ten.
And if I want to show the symbol value.
We cannot use this method.
Let me show you if we try.
So I'm going to duplicate this line and I'm going to change this variable name H.
And if I save this code.
You can see it.
Print undefined.
Class is our key.
Edge is our key, but it works with class.
Then why it is not work with edge.
If we want to show exact single value.
In that case, we don't need to use dot.
We need to use square braces.
So we need to move this variable inside the square braces.
If I save this code you can see now it's written the result.
So you need to remember for normal data type we can use dot.
But for simple data type we need to use square braces.
As you know we can print every object key or value using foreign loop.
So let's use foreign loop and try to print every key in our console.
So I'm going to type four inside the parenthesis.
Let key in user.
User means our object name.
Then inside the curly braces console dot log inside the parenthesis key.
If I run this code it going to print student name user name user class.
But it is going to skip user is because it is a symbol.
So I'm going to save this file.
Now you can see in my console it print name class but it doesn't print each key.
So it's print only to key name in class.
But if I duplicate this line and try to insert another key name gender and his gender is male.
So I'm going to type male.
If I save this file, you can see it prints three key from our object name, class and gender.
But it escape key because it's a symbol.
So we cannot use symbol inside the foreign loop.
And you need to remember it always.
And you know why we use JavaScript object?
Because we convert JavaScript object into a Json object and try to send data to the server.
And if I try to convert this user object into a Json, once again it skip this value.
Let me show you this.
You can see we have four value in our object name, class, age, and gender.
So first I'm going to remove this.
I don't need this for loop.
And now I'm going to convert this object into a Json file.
So I'm going to use console dot log.
Inside the parentheses I'm going to use json Stringified method json dot Stringify.
Inside the parentheses I'm going to provide our object name, which is user.
If I save this file you can see it returns a Json object.
But if you notice edge value is missing.
But if I just print this object in our console let's see.
So I'm going to remove this parenthesis.
And if I save this file you can see you can see my console symbol value.
But in my Json object there is no symbol value.
So we need to remember whenever we use symbol value in our object, we cannot use this value in our
foreign loop.
And also we cannot convert this value into a Json object.
And if you really want to convert it, then we need to convert this symbol value into a string.
As I told you earlier, we use the description method to convert the symbol into a string, and then
we can convert it into a Json object.
We do not directly convert symbol into a Json object.
So basically this is our new datatype symbol symbols mean unique value.
It is rarely used.
I hope now it's clear for you.
What is symbol datatype?
Thanks for watching this video.