And we saw the following code for representation:
In this blog, we will answer the questions we were left with and cover the following topics:
|Boolean, Object||Comparison Operators|
Remember, you can always code along with this article! Log in to codedamn playgrounds and start an HTML/CSS playground for yourself. It is free to use.
You can find all the code files on the playground here.
Object Data Type
First of all, let us figure out a way to represent Jason more neatly in the code. Think about it, what would you do? Think about a structure in real life where all this information is easily available for a single person. If you thought of a student ID card, well then we think alike! An ID card has all the information we would need for a student and it usually has the following format:
If you notice, for the three ID cards that we have, they represent:
- Same information for different individuals
- Have the same format
- Are easily identifiable, i.e., ID = 1, will refer to Jason Bourne and so on.
Notice, a few things:
- The box gets replaced by curly braces
- The ID, gets replaced by the variable name “
- Rest of it, remains the exact same
Isn’t that easy?
Next, let us represent Jason, with all the hobbies!
- Easier to understand and write code
- Can be done for multiple people (read as, objects) like Pichai and Jobs earlier
And don’t worry, we will discuss the square bracket syntax, used to form arrays in a blog soon to come.
The general syntax is very easy:
Yes! You can have objects as values to keys. Read that again!
Let us console.log a few things about Jason from the Object data type.
Alright! So clearly, we can still use the Object data type to produce the same output as we did in the previous article. But the method is slightly different.
Let’s list them down:
- Dot Notation:
The student ‘dot’ key will return the value associated with that key. So
student.name returns “
- Bracket Notation:
student[key] will do the same thing but has a different syntax. So
student[“name”] returns 22.
In the image, you will see that hobbies is followed by
. You will learn more about indexing and arrays in a different blog post but for now, just understand adding the
 returns the first value in the array, which is
Check out the nested object as well:
If education was a separate object, you would have written
education.postgrad to get computer science. Since education is inside
nestedObject, you add another dot before education. You can also do the following:
Phew! That was a lot. If you have made it this far and are still with me, you are amazing!
We just have one more data type to discuss along with comparison operators.
Boolean Data Type
That is it. The boolean data type. A variable is given a value of true or false is referred to as the variable holding a boolean data type.
For instance, in our example:
Larry’s age is less than 25 years, so we set the variable
true, because that is correct. Similarly, we set the
isSalaryMoreThan200K variable to
false because the salary is $120,000!
That is it for the boolean data type! But wait, what should you do with this information? That’s where comparison operators come in.
You would have to use the “
<” symbols for checking more than or less than a certain value.
To check if the salary is equal to 120000, you would have to use the “
==” operator. It basically checks whether the value on the left-hand side is equal to the right-hand side.
Each of these comparisons returns a true or false value! A boolean value.
Okay, then here is another question for you! What do you think will be the output of the following code:
Note very carefully that, while in line 12, 120000 is a
Number data type, in line 16 it is a
String data type because it is inside double-quotes.
String type into
Number type and then return
true! This is called Type Conversion. Try it out.
The issue with this is, in many cases, you might not want JS to act all intelligent and do the work for you.
So to avoid that, you can use the “
When you use the “
false” because a
Number type is not the same as a
It is a good practice to use “
===” wherever possible.
|Not Equal To|
|Strict Equal To|
|Strict Not Equal To|
|Greater Than Equal To|
|Less Than Equal To|
In this blog, we saw a lot of detail about the
Object data type. We also learned about Boolean data types and the Comparison operators and we touched upon type conversions. And that’s pretty much it for this article.
In the next one, we will understand Type Conversions in greater detail and delve into some simple arithmetic operations like addition, subtraction, multiplication, and division.
Till then, practice, practice, and practice on codedamn playgrounds.
Free money-back guarantee
Unlimited access to all platform courses
100's of practice projects included
ChatGPT Based Instant AI Help (Jarvis)
Structured Full-Stack Web Developer Roadmap To Get A Job
Exclusive community for events, workshops
Sharing is caring
Did you like what Rishabh Mittal wrote? Thank them for their work by sharing it on social media.