38

Possible Duplicate:
Difference between using var and not using var in JavaScript

sometime, I saw people doing this

for(var i=0; i< array.length; i++){
 //bababa

}

but I also see people doing this...

for(i=0; i< array.length; i++){
 //bababa

}

What is the different between two? Thank you.

Robin Dinse
  • 1,491
  • 14
  • 20
Tattat
  • 15,548
  • 33
  • 87
  • 138

2 Answers2

76

The var keyword is never "needed". However if you don't use it then the variable that you are declaring will be exposed in the global scope (i.e. as a property on the window object). Usually this is not what you want.

Usually you only want your variable to be visible in the current scope, and this is what var does for you. It declares the variable in the current scope only (though note that in some cases the "current scope" will coincide with the "global scope", in which case there is no difference between using var and not using var).

When writing code, you should prefer this syntax:

for(var i=0; i< array.length; i++){
    //bababa
}

Or if you must, then like this:

var i;
for(i=0; i< array.length; i++){
   //bababa
}

Doing it like this:

for(i=0; i< array.length; i++){
   //bababa
}

...will create a variable called i in the global scope. If someone else happened to also be using a global i variable, then you've just overwritten their variable.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • Hi, in your example do you have to register the global variable I somewhere else for it to be declared global scope, or does anything not declared with var be understood as global variable implicitly by the javascript engine? – Oh Chin Boon Jul 30 '11 at 06:10
  • @Chin - Anything not declared with `var` will be implicitly understood as a global variable in JavaScript, no special registration is required. – aroth Jul 30 '11 at 06:13
  • Thanks for the good tip @aroth. keeping my variables local also helps me to easily walk through the code while debugging in Google chrome. – klewis Aug 23 '13 at 11:53
0

technically, you never HAVE to use it, javascript will just go merrily on its way--using your variables even if you dont declare them ahead of time.

but, in practice you should always use it when you are declaring a variable. it will make your code more readable and help you to avoid confusion, especially if you are using variables with the same name and different scope...

var n = 23;
function functThat( )
{
   var n = 32; // "local" scope
}
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • care to explain the down vote? – Muad'Dib Jul 30 '11 at 06:09
  • 1
    I didn't downvote it, but my guess is because the statements are just all wrong. You really don't want global variables popping up and being changed at random. yes, Virginia, Javascript really sucks. But to help alleviate that you use var everywhere to make your code less buggy. Not really having anything to do with readable. – Keith Jul 30 '11 at 06:19
  • 5
    The question asks what the difference between using var and not using var is but this answer just gives advice on when to use it without ever explaining what that difference actually is. – Quentin Jul 30 '11 at 06:25