[thelist] JavaScript: Why can't this function see...

Hershel Robinson hershelr at netvision.net.il
Thu Mar 10 12:46:37 CST 2005

> // Ex 3. Create a global variable *without* a declartion.
> function f()
> {
>      var x = 2;
>      alert("In: " + x);
> }
> f();
> alert("Out: " + x);

This example does NOT work for me on IE 6 nor Firefox 1.0 on Win XP
actually. I do not think such code should work either. As far as I know (and
I may well be wrong) you have declared x to be local to function f.

Even if it does work on other browsers, your example actually declares the
variable with the var keyword. Tom's code did not do that in any place, save
the one he threw in as a hack to get around the problem--and that one he
wanted to remove.


More information about the thelist mailing list