Blog Archives

Understanding JavaScript’s scopes

Sometimes, I see odd JavaScript-code, like this:

1
2
3
4
5
6
7
8
9
// declares var "i":
for (var i=0; i<2; i++) {
    // ...
}
 
// redeclares var "i":
for (var i=0; i<2; i++) {
    alert("I don't understand JavaScript's scopes");
}

The author of this code (obviously) didn’t really understand JavaScript’s scoping rules.

Tagged with: , ,
Posted in JavaScript