It occurred to me, that I have no idea what the scope of
var foo='bar',
baz = 'bar';
is.
Obviously foo is locally scoped, but is the var keyword necessary on baz to scope it locally, or is my example already locally scoped?
It occurred to me, that I have no idea what the scope of
var foo='bar',
baz = 'bar';
is.
Obviously foo is locally scoped, but is the var keyword necessary on baz to scope it locally, or is my example already locally scoped?
They will both end up in the same scope.
var foo = 'bar',
baz = 'bar';
Is just short for:
var foo = 'bar';
var baz = 'bar';
So within a function for instance, both foo
and baz
will become local variables, even if you only declare var
once.
Tools like JSLint actually expect the var
keyword to be used only once, so if you want to comply with that, you should use the first example.
This is a very interesting question, indeed. JavaScript is full of border cases.
In this blog post: http://scribu.net/blog/javascript-var-keyword-for-php-developers.html, The authors gives some examples of the impact of using or not the 'var' keyword.
To make a long story short,