This question already has an answer here:
Is "var" optional?
myObj = 1;
same as ?
var myObj = 1;
I found they both work from my test, I assume var
is optional. Is that right?
This question already has an answer here:
Is "var" optional?
myObj = 1;
same as ?
var myObj = 1;
I found they both work from my test, I assume var
is optional. Is that right?
There's a bit more to it than just local vs global. Global variables created with
var
are different than those created without. Consider this:Based on the answers so far, these global variables,
foo
,bar
, andbaz
are all equivalent. This is not the case. Global variables made withvar
are (correctly) assigned the internal[[DontDelete]]
property, such that they cannot be deleted.This is why you should always use
var
, even for global variables.var
is optional.var
puts a variable in local scope. If a variable is defined withoutvar
, it is in global scopeand not deletable.edit
I thought that the non-deletable part was true at some point in time with a certain environment. I must have dreamed it.
They mean different things. If you use
var
the variable is declared within the scope you are in (e.g. of the function). If you don't usevar
, the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches. It is then very similar to a global variable. However, it can still be deleted withdelete
(most likely by someone else's code who also failed to usevar
). If you usevar
in the global scope, the variable is truly global and cannot be deleted.This is, in my opinion, one of the most dangerous issues with javascript, and should be deprecated, or at least raise warnings over warnings. The reason is, it's easy to forget
var
and have by accident a common variable name bound to the global object. This produces weird and difficult to debug behavior.Consider this question asked at StackOverflow today:
Simple Javascript question
A good test and a practical example is what happens in the above scenario...
The developer used the name of the JavaScript function in one of his variables.
What's the problem with the code?
The code only works the first time the user clicks the button.
What's the solution?
Add the
var
keyword before the variable name.They are not the same.
Undeclared variable (without
var
) are treated as properties of the global object. (Usually thewindow
object, unless you're in awith
block)Variables declared with
var
are normal local variables, and are not visible outside the function they're declared in. (Note that Javascript does not have block scope)I just found the answer from a forum referred by one of my colleague. If you declare a variable outside a function, it's always global. No matter if you use var keyword or not. But, if you declare the variable inside a function, it has a big difference. Inside a function, if you declare the variable using var keyword, it will be local, but if you declare the variable without var keyword, it will be global. It can overwrite your previously declared variables. - See more at: http://forum.webdeveloperszone.com/question/what-is-the-difference-between-using-var-keyword-or-not-using-var-during-variable-declaration/#sthash.xNnLrwc3.dpuf