As we said before, arrays can take any type of data. And it can even take objects. And it's important to know how to add objects to an array. Because it's going to be super powerful, because adding all these kind of objects into one single variable will make your life much easier. So let's take a look. Suppose that you have an array called employees, employees, and instead of just typing the name of the game, the name of these employees, john, Kay, etc, etc.
Suppose that we want to have more information about that single employee. So for example, john, we want to have his ID, his age, his height, and even more more information and this is very important because Suppose that you are traveling somewhere, you would have to have a passport. And that passport, for example, has a lot of information about you your name, your age, your date of birth, many things. And these things are recorded in a database in a computer. And in order to identify you, there has to be some kind of object that contains all of these information about about you. Therefore, we need to mimic this and create our own array of objects.
So let's learn how to create an array of objects. So instead of just adding strings, what I'm going to add here is as we learned before, here, if we create just a single object of our person one equals two kale replaces and then here I'm going to tell you the key value pairs mean JOHN, and then age, and then 25, and then maybe anything else you can think of maybe the height, maybe the date of birth, maybe the nationality, anything you want. But for the sake of simplicity, I'm going to use the height. I'm going to also i'm going to only use three elements, three properties of this object. So here 180. So here we have three elements in this object.
And in order to add this object inside this array, we would type person one, right? But instead of creating this explicitly, and storing it into a single variable, what I'm going to do is that I'm gonna copy this complete line, and E and I'm going to delete this and I'm going to paste it here. And as you can see, we have now an employee's array that has a single object inside a single element, I am no longer in need of this line of code, right, I'm going to delete this line of code. So now our life is much easier. And we only need one variable, of course here in order. In order to add more elements to this array, what I'm going to do is add comma, and then paste this again, and change these information for example key and age, maybe 23, height 170 and so on and so forth.
If you want more elements, just add comma and add your object. So now we have this array, which has two elements. But the problem is that how are we how are we going to access a specific element a specific problem 30 have one of these objects? Well, the answer is, in order to access one, one complete element from an from an array, what we learned is that you, you would type in, you would type employees, and then square brackets, and then the index of that element. So this complete object is now an element. So this is element one, or item one.
And this complete object now is element two, or I think two. So here I want to access john, just john. If I type zero, and use the console, dot log, and I only print this. What I'm going to get is that we're going to get the complete object, the complete information name, age and height. But if I want only the name, what they would do now is they would call The name the name property by the dot notation. So here I'm going to use name.
And save and run. And as you can see, I get only the name, only the name. If I want the age only I'm going to use, I'm going to call the age, save on run, I'm going to get 25. But if I want the second element, we only change the index. So here's index from zero to one. And I'm going to change the age to the name.
And if I save and run, I'm gonna get Kate. So this is how you can create objects inside an array, and it's called array of objects. And this is how we can access a specific property of an object that lies inside an array.