JavaScript help

houssam_ballout

New Member
Hello all,
can some body explain, how this code works/ (more on var x)
thanks
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"

for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
 
x is just used as a counter. The for loop has x start at 0 and increments it until it is greater than the number of elements in mycars.

Actually, if memory servers that code is very similar to the one w3schools uses for for..in loops.
 
Back
Top