There are other four loop forms that we are going to learn. The first one is called or in, which is used specifically for objects. And another one called for of loop which is used for arrays. So let's now talk about for in loop. A for in loop is designed specifically to loop through objects. So for example, we have here, a car object that has three properties, name, year and color.
And we want to loop through these three properties or this object, instead of just calling the name and then we go again and call the year and go to another line and call the, the color we can just loop through this object, getting all of its properties and printing them or using them in other functions and other parts of our program. But how would we do that? The way to do so is by using for in loop two, Let's now get an example. So now suppose that we have this object, a car object that has these these three properties. And we want to get all of these properties. The way that we learned how to get properties from an object is by using the dot notation or bracket notation.
So let's just review console dot log. And then what we would do is just use car dot name. And then we save our program. And as you can see, we get Tesla or we could just use square bracket and we pass the key which is name and save and run our program and we get Tesla and as you can see, it would be Very difficult to write or call each property if we, especially if we have many, many properties inside an object. So in order to make everything easy, we use the for in loop. And to use the for in loop, you just type four, and then parentheses, and then create a variable, any variable and then and it's conventional to be key.
It's called key, and then in, and then the name of that object, which is car, and then curly braces. And then inside these curly braces, you add the code that you want to loop through these properties. And I'm going to use console. Con. So and then log, and then inside of a console log, you should type car and then square brackets, and then you pass the key because here the meaning This line of code of this lead key in in car, it means that let each property that we are going to loop through, let it be a key equals to key. So for example, each time it's going to get to this object, and each time it's going to get the first the first property and gets only the key and store it into a variable called key.
And then it's going to get to the second line or second property, and then it's going to get the year and here should be year. And it's going to get the year and store it into the key. So in order to access the values of these keys, we use these square brackets, we don't use the dot notation here, we use the square brackets, and we pass that key. So that key for the first iteration, it's going to be equal to name. The second iteration, it's going to be equal to a year, and the third iteration is going to be equal to color. So let's now save And as you can see, if I save and run, I get this law 2020 and black, because each time the key will be equal to a property, the first one the first time, it's going to be equal to names the second time the year, and then the color.
And you can see how easy it is to access all of the properties of an object just by using by using the for in loop, which is designed specifically for objects. But here if I try to use the dot notation, dot and then key, it's not going to work because if I save and run, I will get undefined. So this is why it's important to use square brackets when you use for loop but if you don't, if you're not using for a loop, it's better to use the dot notation which is car that car dot and then the property name which is the the name of the property which is name or or color. So to recap, in order to loop through properties of an object, what you would do is you would use for in loop