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?
Variables can be declared and initialize without the var keyword. However, a value must be assigned to a variable declared without the var keyword. The variables declared without the var keyword becomes global variables, irrespective of where they are declared.
This means that any variable that is declared with var outside a function block is available for use in the whole window. var is function scoped when it is declared within a function. This means that it is available and can be accessed only within that function.
If you declare a variable, without using "var", the variable always becomes GLOBAL.
"Optional" is perhaps an unfortunate choice of words, as var is optional in the sense that an assignment statement can be successfully interpreted with or without it, but not optional in the case where you want to explicitly declare a variable in local scope.
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 use var
, 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 with delete
(most likely by someone else's code who also failed to use var
). If you use var
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.
This is one of the tricky parts of Javascript, but also one of its core features. A variable declared with var
"begins its life" right where you declare it. If you leave out the var
, it's like you're talking about a variable that you have used before.
var foo = 'first time use'; foo = 'second time use';
With regards to scope, it is not true that variables automatically become global. Rather, Javascript will traverse up the scope chain to see if you have used the variable before. If it finds an instance of a variable of the same name used before, it'll use that and whatever scope it was declared in. If it doesn't encounter the variable anywhere it'll eventually hit the global object (window
in a browser) and will attach the variable to it.
var foo = "I'm global"; var bar = "So am I"; function () { var foo = "I'm local, the previous 'foo' didn't notice a thing"; var baz = "I'm local, too"; function () { var foo = "I'm even more local, all three 'foos' have different values"; baz = "I just changed 'baz' one scope higher, but it's still not global"; bar = "I just changed the global 'bar' variable"; xyz = "I just created a new global variable"; } }
This behavior is really powerful when used with nested functions and callbacks. Learning about what functions
are and how scope works is the most important thing in Javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With