Javascript: For (var property in dog)

5:14 PG 0 Comments A+ a-


var dog = {
species: "bulldog",
age: 3,
color: brown
};

for(var property in dog) {
console.log(property);
}


The words var and in are keywords. We need them to always be there. We can replace dog with any object that we want the for-in loop to run through. And you can think of property as a placeholder variable. You can use any word you want here.

In English, what is the code doing? It says: Assign the first property of the dog object to the variable property. Run the code (here, it is to print property to console). Then assign the second property of the dog object to the variable property. Again, run the code in the curly brackets. Keep repeating this until all the properties of the dog have been assigned to property.