Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use var in Javascript [duplicate]

Tags:

javascript

var

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.

like image 885
OscarRyz Avatar asked Mar 22 '11 22:03

OscarRyz


People also ask

When should you use VAR in JavaScript?

Always declare JavaScript variables with var , let , or const . The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browser, you must use var .

When should I use var instead of let?

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

Why is var needed?

Risk managers use VaR to measure and control the level of risk exposure. One can apply VaR calculations to specific positions or whole portfolios or use them to measure firm-wide risk exposure.

Why VAR is not recommended?

In Javascript, it doesn't matter how many times you use the keyword “var”. If it's the same name in the same function, you are pointing to the same variable. This function scope can be a source of a lot of bugs.


4 Answers

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
like image 63
Bodman Avatar answered Oct 16 '22 01:10

Bodman


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'
like image 36
Sam Dufel Avatar answered Oct 16 '22 01:10

Sam Dufel


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

like image 45
It Grunt Avatar answered Oct 15 '22 23:10

It Grunt


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.

like image 35
Agonych Avatar answered Oct 16 '22 01:10

Agonych