Skip to content Skip to sidebar Skip to footer

For Loop Repeats First Loop Twice

I have this for loop, and it seems to be repeating the first loop twice (x=0) and then not doing the last one (x=2) for (x=0;x<=2;x++) { if (document.getElementById(

Solution 1:

that is literally the only spot I use the variable x on any page.

It works for me.

for (x=0;x<=2;x++)
{
  alert(x);
}

You can test it at console.

Solution 2:

I don't think you want the variable x to be global in scope. Try it with the "var" keyword:

for (var x=0;x<=2;x++)
...

I can paste this in my address bar and it will produce 0, 1, 2.

javascript:for (var x=0;x<=2;x++) {alert(x);}

I tried it in IE, FF and Chrome.

Post a Comment for "For Loop Repeats First Loop Twice"