This project has moved. For the latest updates, please go here.

For loop initializer resets to 0 after first iteration in specific situation

Dec 13, 2016 at 5:38 PM
Ran into an interesting issue. I'm running an older version of Jurassic so I haven't verified if this still exists or not.

function run()
{
console.log(" ");
console.log("for(var i = 1; i < 3; i++)");
for(var i = 1; i < 3; i++)
{
    console.log(i)
    continue;
}

console.log(" ");
console.log("for(; i < 3; i++)");
var i = 1;
for(; i < 3; i++) 
{
    console.log(i)
    continue;
}
}
run();

The first for loop incorrectly outputs: 1, 1, 2,

The second for loop correctly outputs: 1, 2,

A couple of notes. This only happens if the code is in a function. If it's in the main script it works as expected. Also the issue only occurs when there's a continue; statement in the for loop body.

It seems to be something with the continue; statement when the for loop is encapsulated in a function.
-Andy