Nov
25
JavaScript - Lack of Block-Level Scoping
November 25, 2007 |
Counter to most every other language, in JavaScript, everything was in the global scope, it is lack of block scope. While in other programming language, variables are generally scoped only at the level they are required.
So when you run the following script, instead of getting 3,2,1, you will get 3,3,3.
function test() {
var i = 1;
if (1) {
var i = 2;
if (1) {
var i = 3;
alert(i);
}
alert(i);
}
alert(i);
}
You can test it out at the url below:
http://www.lab.highub.com/javascript/global-scope.html
Similar Posts
- None Found
Comments
1 Comment so far



































[...] check the full story here [...]