Jump to content

beginner javascript question


eddie21leeds

Recommended Posts

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!

Link to comment
Share on other sites

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 />" );
}

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.