Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2013 13:56
Show Gist options
  • Save anonymous/4443628 to your computer and use it in GitHub Desktop.
Save anonymous/4443628 to your computer and use it in GitHub Desktop.
A weird behaviour of JavaScript about variables with same name management.
// Weird thing:
// ************
//
// The following code does not throw any error, despite i is declared twice
// and I am using "use strict".
//
// So I was expecting the i in the inner for loop to be a different variable
// than the other i (and that it would override it in its own scope).
//
// But it does not: The log is:
// > 01
// when I was expecting more:
// > 0101
(function(){
"use strict";
var str = '';
for (var i = 0; i < 2; i++)
for(var i = 0; i < 2; i++)
str += i;
console.log(str);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment