Clear your script and add this bit of code.
var people = new Array( 'James', 'Bob', 'Billy' );
for (x in people)
document.write( people[x] + '<br /> ' );
The variable does not represent the value, but the index. So, inside our loop, we’re using x as the index and not outputting it out itself. If we did, you would just get a bunch of numbers. You should get the following outputted.
Let’s take a look at how to use the for in loop with objects. Clear your script and add this bit of code.
this.color1 = 'red';
this.color2 = 'blue';
this.color3 = 'green';
var foo = new colors();
for (y in foo)
document.write( foo[y] + '<br /> ' );
First, we create a class and inside this class are a couple of properties. Then, we create an instance. Lastly, we use the for in loop to loop through the object. Like last time, y represents the name of the property. It does not represent the value.
If you refresh your document, then you should get something like this.
The for in loop is very confusing. The only way to really get it in your head is to practice using it. If you would like more information about the for in loop, then go here.