Submodule 12.2: JavaScript Objects

for/in Statement

In order to iterate over objects' properties we cannot use the classic for loops.

The syntax that allows as to iterate over all the properties of an object is:

for (var key in object)

var key can take any name. 

Example:

var darkChoco = {
cacao:"80%",
milk:"0%",
grams:100,

};

In order to iterate over this object we would write:

 for (var x in darkChoco) {
console.log ( x + ":" + darkChoco[x] );
} 

Exercise

Follow the next link. Right-click, select inspect and view the result in the Console tab. Next, click the "Sources" tab and click the HTML file to see the code:

 for/in Statement