Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using 'var' to declare variables optional? [duplicate]

Tags:

javascript

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?

like image 349
Ray Lu Avatar asked Mar 21 '10 01:03

Ray Lu


People also ask

Is it mandatory to use the var keyword when declaring a variable?

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.

What does variable is declared as VAR mean?

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.

What happens if we declare a variable * without * the VAR let keyword?

If you declare a variable, without using "var", the variable always becomes GLOBAL.

Is VAR optional in JS?

"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.


2 Answers

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.

like image 60
Stefano Borini Avatar answered Sep 29 '22 03:09

Stefano Borini


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.

like image 25
deceze Avatar answered Sep 29 '22 02:09

deceze