Created
January 3, 2013 13:56
-
-
Save anonymous/4443628 to your computer and use it in GitHub Desktop.
A weird behaviour of JavaScript about variables with same name management.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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