
What they don't teach you in (W3)school: JavaScript variable scoping - DjDarkman
http://blog.istvan-antal.ro/2011/01/what-they-dont-teach-you-in-w3school-javascript-variable-scoping/
======
thezilch
FUD.

w3schools doesn't, at all, make the claims this author is stretching for. In
fact, on their JS Functions page (<http://w3schools.com/js/js_functions.asp>),
w3schools state precisely what the author illustrates:

 _The Lifetime of JavaScript Variables_

 _If you declare a variable, using "var", within a function, the variable can
only be accessed within that function. When you exit the function, the
variable is destroyed. These variables are called local variables. You can
have local variables with the same name in different functions, because each
is recognized only by the function in which it is declared._

 _If you declare a variable outside a function, all the functions on your page
can access it. The lifetime of these variables starts when they are declared,
and ends when the page is closed._

~~~
DjDarkman
FACT.

There is a screenshot in the post, on your link it may be right, but on the
link referenced in the post, it's wrong.

~~~
originalgeek
Actually, the statements in the image you posted are all outside of functions,
and thus in the global context, so the statement that they are equivalent is
true.

~~~
DjDarkman
The problem is that particular w3schools page does not indicate that, they
simply state that they are the same, without even slightly mentioning that
it's because they are not in a function. My article shows off this difference.
In short the page is misleading, because it doesn't state that the two are
only interchangeable in those two examples, but not in in every situation.

