eddie21leeds Posted January 22, 2009 Share Posted January 22, 2009 hi! i am currently working my way through the w3 schools javascript tutorials and i have come across some thing that i dont understand in the for...in statement lesson: <html> <body><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></body> </html> i don't understand how the variable x works... could the x be any letter? or is it a special javascript thing? i know that this means means "while there is a value in the array - write it" but i cant quite grasp how it works... also - how could i change the code so that just the first two values in he array are displayed? - this may help me understand it better. thanks in advance! Quote Link to comment Share on other sites More sharing options...
glenelkins Posted January 22, 2009 Share Posted January 22, 2009 x in this case is an incrementing variable. so basically what this loop is doing is going through each item in the array while x is incremented: 0,1,2,3,4 etc... until the end of the array. the x is then used as a "pointer" to the location within the array. so for example: var x = 0; document.write ( mycars[0] + "<br />" ); this would display "Saab". The loop could be written in a longer form as such: for ( var x = 0; x < mycars.length; x++ ) { document.write ( mycars[x] + "<br />" ); } Quote Link to comment Share on other sites More sharing options...
eddie21leeds Posted January 22, 2009 Author Share Posted January 22, 2009 ok thanks... i think i get it - so for (x in mycars) is passing an empty x to the array to be incremented?? if anyone has the time i would be interested to see a more complex for...in loop to get a better idea of what they can do, the example on w3 schools looks pretty simple... thanks! Quote Link to comment Share on other sites More sharing options...
phparray Posted January 22, 2009 Share Posted January 22, 2009 x in this case is an incrementing variable. In this case sure you can say that but not because the first argument is and auto incrementing value. The first argument in the "for in" function is the key values. Consider if the keys where not 0 throught 3. <html> <body><script type="text/javascript"> var x; var mycars = new Array(); mycars[0] = "Saab"; mycars[5] = "Volvo"; mycars[8] = "BMW"; for (x in mycars) { document.write(mycars[x] + "<br />"); } </script></body> </html> I this example the x is qual to 0,5, and 8 in that order but the output is identical. Quote Link to comment Share on other sites More sharing options...
glenelkins Posted January 22, 2009 Share Posted January 22, 2009 The first argument in the "for in" function is the key values. ah yeh! i didnt think of that lol...good point, my bad! Quote Link to comment Share on other sites More sharing options...
eddie21leeds Posted January 22, 2009 Author Share Posted January 22, 2009 thanks!! it's all becoming clear now!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.