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

Keith Gaughan keith at digital-crew.com
Thu Mar 10 13:10:39 CST 2005


Hershel Robinson wrote:
>>// 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.

Sorry. The 'var' shouldn't have been there. My bad, but it still
demonstrates the point.

Unlike the likes of PHP, variables are global unless marked otherwise.

K.


More information about the thelist mailing list