For...in Loop
So far you have learned about three kinds of loops in JavaScript:
-
forloops, -
whileloops, and -
do-whileloops.
With all these loops, we can repeat an action a number of times.
But we have two more kinds of loops in JavaScript, and we use them to iterate over the properties of an object or elements in an array.
Let me show you. In this lecture, we're going to look at the for...in loop.
The for...in Loop for Objects
-
Let's say we have an object like
person, with two properties:name('Mosh') andage(30).const person = { name: 'Mosh', age: 30 }; -
Let's say we want to display all the properties of this
personobject. That's when we use thefor...inloop. -
So,
for, and now in parentheses, unlike theforloop that we learned about earlier, we don't have three parts. We don't have that initial expression followed by a semicolon, the condition followed by the increment part. It looks a little bit different, let me show you. -
We have
let key in person. In every iteration, thiskeyvariable will hold one of the properties in thispersonobject. Let me show you.
-
Here I'm going to do a simple
console.logofkey.const person = { name: 'Mosh', age: 30 }; for (let key in person) { console.log(key); } -
Save the changes.

Expected Output:
name ageSo in the first iteration,
keyisname, and in the second iteration, it isage.
Displaying Property Values
-
Now, what if you want to display the value of each property next to it?
-
Well, earlier you learned that there are two ways to access the properties of an object.
-
We can use the dot notation, which looks like this:
person.name. -
Or we can use the bracket notation:
person['name'], passing the name of the target property as a string.
-
-
Earlier I told you that we use the bracket notation when we don't know ahead of time, at the time of writing code, what property we're going to access. Perhaps the name of the target property is calculated at runtime.
-
Here is a real example. When we iterate over the properties of the
personobject, in each iteration, the value ofkeyis going to be different.
-
So, here we cannot use the dot notation to display the value of this property. In other words, we can't do something like this:
const person = { name: 'Mosh', age: 30 }; // This will not work for (let key in person) console.log(key, person.key);...because we don't have a property called "key" in the
personobject. -
So that's when we use the bracket notation. We add square brackets and pass
keyas the name of the target property. -
Now let me delete this stuff here. Let's log the
keyand the value using bracket notation.const person = { name: 'Mosh', age: 30 }; for (let key in person) { console.log(key, person[key]); } -
Save the changes.

Expected Output:
name Mosh age 30So you can see the value of
nameis Mosh and the value ofageis 30.
Key Takeaway:
- This is the
for...inloop, and we use it to iterate over the properties of an object.
Using for...in with Arrays
-
We can also use these to iterate over an array. But it's not an ideal way; in the next lecture, I'm going to show you a better way. But let's see how that works before we finish this lecture.
-
I'm going to define an array called
colorswith three values: 'red', 'green', and 'blue'.const colors = ['red', 'green', 'blue']; -
Now we can use the
for...inloop to iterate over this array.for (let index in colors) { } -
Note that I named this loop variable
index, because in each iteration, thisindexvariable will be set to the index of one of the elements in this array. So it's going to be 0, 1, and 2. -
Let's take a look. So,
console.log(index).const colors = ['red', 'green', 'blue']; for (let index in colors) { console.log(index); } -
Save the changes.

Expected Output:
0 1 2So, we get 0, 1, and 2.
-
Now, if you want to get the element at a given index, once again we use the square bracket notation. So,
colors[index].const colors = ['red', 'green', 'blue']; for (let index in colors) { console.log(index, colors[index]); } -
Save the changes.

Expected Output:
0 red 1 green 2 blueAnd now we can see each element in our
colorsarray.
Preview: The for...of Loop
Now, starting from ECMAScript 6 (or ES6), which is the modern version of JavaScript, we have a new kind of loop that is called the for...of loop, and that is an ideal way to iterate over arrays. That's what you're going to learn in the next lecture.