63

Maybe pretty easy question.

Where should I use var keyword in JavaScript. It seems to me using it or not have the same effect ( but of course I'm still learning the language )

For instance these both seems the same to me:

(function(){
  var a = "mundo"
  alert("Hola, " + a )
})()

and

(function(){
  a = "mundo"
  alert("Hola, " + a )
})()

But of course there must be a more complex example where the difference shows up.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569

4 Answers4

96

When you use var , you are instantiating a variable in the current scope. This will also prevent access of variables named the same in higher scope, within the current scope.

In your first example, 'a' is being instantiated and set within the function scope. In your second example, 'a' is being set outside the function scope due to lack of var

With var:

var a = "A"
(function(){
  var a = "B"
  alert(a) //B
})()

alert(a); //A

Without var:

var a = "A";
(function(){
  a = "B"
  alert(a) //B
})()

alert(a) //B
Bodman
  • 7,938
  • 3
  • 29
  • 34
  • would `var` declare it always? Is that what you mean? – OscarRyz Mar 22 '11 at 22:48
  • 1
    Exactly, var declares the variable in that current scope. – Bodman Mar 22 '11 at 22:49
  • @SNag Your edits go into a review queue, so it's important that you make edits sufficiently large so as not to waste reviewer's time. I was very unconvinced that the edit met this bar, but I didn't want to flat-out reject it (seeing as you said it's important). I wasn't trying to steal the edit, though. Apologies that it looked like that. – Veedrac Jun 03 '14 at 07:07
  • @SNag I've looked at some of your suggested edits and although [some of them are great](http://stackoverflow.com/review/suggested-edits/3023938), [others should wait](http://stackoverflow.com/review/suggested-edits/4412153) until you have the rep. to skip the edit queue (although that one I'd personally leave as a comment until the author makes the change). And [some should really never happen](http://stackoverflow.com/review/suggested-edits/4462823) anyway, as they don't actually improve anything. – Veedrac Jun 03 '14 at 07:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54974/discussion-between-veedrac-and-snag). – Veedrac Jun 03 '14 at 10:02
16

Using var:

var a = 'world';   
myfunction = function(){
  var a = "mundo"
  alert("Hola, " + a )
}

myfunction();  //alerts 'hola, mundo'
alert(a);  //alerts 'world';

Not using var:

var a = 'world';   
myfunction = function(){
  a = "mundo"
  alert("Hola, " + a )
}

myfunction();  //alerts 'hola, mundo'
alert(a);  //alerts 'mundo'
Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
6

I think that you need to refresh yourself on Javascript object scopes.

Using the "var" keyword will place your variable at the top-most (global) scope. This means that if a function uses the same variable, the "var" variable you declared will overwrite the (non-var) variable in your function... JavaScript Scopes

It Grunt
  • 3,300
  • 3
  • 21
  • 35
1

if var not used inside function, JS will look for it above, so in case you use save vars in different functions they might conflict. It always worth to use a var if you are defining new variable.

Agonych
  • 330
  • 4
  • 4