var car = {

make: “Ford”,

model: “Fiesta”,

colour: “Blue”

};

var x;

for(x in car) {

console.log(car[x]);

}

As you can see, after we have defined our object, we first declare a new variable named x – this is going to be used during our loop. Then we use a standard for keyword, followed by the parenthesis which contains the code ‘x in car’. In this loop, the value of x is set to that of each property, so when we step into the actual code to be executed, inside the curly braces, we can see that we are making use of the x variable to pick off that property from the car object and display it in the console.

Similarly to the standard for loop, we can write any code that we wish inside this block. The loop will automatically iterate through the whole object until it reaches the end of the properties.

This loop is an automatic loop, which requires very little code in order to achieve a very powerful loop.

The while loop

The while loop is similar in nature to the for loop, in that they both run a block of code if a condition is true. However, the way they go about this is slightly different between the two.

With the while loop, we specify our condition at the top, just like in the for loop. However, that’s all we need to do. We simply define the criteria that we want to be true for the code to run, then we handle the variable changing inside the block. This allows us a bit more flexibility than when using the for loop, as we can decide exactly where we would like to place our variable incrementing statement, just as we can see below: