Nov

25


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

Comments

Name (required)

Email (required)

Website

Speak your mind

1 Comment so far

  1. hosthg » Blog Archive » JavaScript - Lack of Block-Level Scoping on November 25, 2007 8:29 pm

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

Sponsors




Links