If you’ve seen javascript objects before, you know they follow a certain form:
{property1: value1, property2: value2}
Imagine this was the object called “plan 1” :
var plan1 = {apple: 1, banana: 2, coconut: 3};
The properties of the “plan1” object are apple, banana, and coconut.
A “for…in” loop is used specifically for objects and it loops through the object.
for (var prop in plan1) {
The “for” and the “in” part should be clear. That’s just the syntax for a “for…in” loop.
The “var prop” part just declares a new variable called “prop” that’s to be used specifically for the loop.
You don’t have to call it prop, you can call it whatever you want.
The “plan1” part is the actual object that you want to loop through.
The loop will run once for each item in the object, so going back to our “plan1” example,
var plan1 = {apple: 1, banana: 2, coconut: 3};
In this case, because the object has 3 items, the loop will run three times.
Each time it runs, the variable you declared (in our case, the variable is “prop”) will have it’s value changed to the property of whichever item the loop is processing. In other words, the first time the loop runs, the variable “prop” will equal “apple”, the second time prop will equal “banana”, and the third time, “prop” will equal “coconut”.
This part should now make sense:
listOfProperties.push(prop);
It’s pushing each property name (“apple”, “banana”, and “coconut”) into the “listOfProperties” array.
At the end, the “listOfProperties” array should equal [“apple”, “banana”, “coconut”]
Hopefully that’s clear.
Here’s a resource on “for…in” loops: