Skip to content

Instantly share code, notes, and snippets.

@hozaka
Created June 11, 2010 05:50
Show Gist options
  • Save hozaka/434099 to your computer and use it in GitHub Desktop.
Save hozaka/434099 to your computer and use it in GitHub Desktop.
var i = "initialized";
function test() {
var i = 0;
for (i in [1, 2, 3]) { console.log(i) };
}
test();
console.log(i);
@huandu
Copy link

huandu commented Jun 18, 2010

Line 4 narrows the scope of var i. If you remove that line, the global var i will be changed after test() is called.

It's a feature in ECMAScript to change global variable value in for..in loop like this according to its specification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment