Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is var necessary when declaring Javascript variables? [duplicate]

Possible Duplicate:
Javascript: is using 'var' to declare variables optional?

When creating variables in javascript is adding "var" before the variable name a must?

For example instead of

var message = "Hello World!"

can I use

message = "Hello World!"

?

I notice that scripts like Google Adsense don't use var

Example:

google_ad_width = 160;
google_ad_height = 600;
google_color_border = "000000";
google_color_bg = "ffffff";
like image 311
V777 Avatar asked Oct 08 '10 16:10

V777


People also ask

Is it necessary to use VAR in JavaScript?

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. Visit Variable Scope in JavaScript to learn about it.

Is it necessary to use var keyword while declaring variable yes or no?

Using var outside a function is optional; assigning a value to an undeclared variable implicitly declares it as a global variable (it is now a property of the global object). The difference is that a declared variable is a non-configurable property of the global object while an undeclared is configurable.

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.

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.


6 Answers

If you don't declare a variable (explicitly creating it in the current scope) using var, let or const then (in non-strict mode) you create an implicit global.

Globals are a fantastic way to have different functions overwriting each other's variables (i.e. they make code a pain to maintain).

If you use var, the scope of the variable is limited to the current function (and anything inside it — it is possible to nest functions).

(const and let scope constants and variables to the current block instead of the function, this usually makes variables even easier to manage than var does.)

Google Adsense uses globals because it splits scripts into two distinct parts (one local and one remote). A cleaner approach would be to call a function defined in the remote script and pass the parameters as arguments instead of having it pick them up from the global scope.


Modern JS should be written in strict mode which bans implicit globals (preferring to explicitly declare them at the top level instead, thus prevent accidental globals when a variable name is typoed).

like image 80
Quentin Avatar answered Oct 03 '22 16:10

Quentin


Yes, you should always use var.

Not using var has two major drawbacks:

  • Accessing a variable within a function that is not defined within that function will cause the interpreter to look up the scope chain for a variable with that name until either it find one or it gets to the global object (accessible in browsers via window) where it will create a property. This global property is now available everywhere, potentially causing confusion and hard-to-detect bugs;
  • Accessing an undeclared variable will cause an error in ECMAScript 5 strict mode.

Also, not using var for global variable is not exactly the same as using var: when using var, the property it creates on the global object has the internal DontDelete attribute, which is not the case without var:

// Next line works in any ECMAScript environment. In browsers, you can
// just use the window object.
var globalObj = (function() { return this; })();

var x = 1;
delete globalObj.x;
alert(x); // Alerts 1, x could not be deleted

y = 2;
delete globalObj.y;
alert(y); // Error, y is undefined
like image 22
Tim Down Avatar answered Oct 03 '22 16:10

Tim Down


From http://www.updrift.com/article/to-var-or-not-to-var-my-javascript

  1. For global variables, it doesn’t matter, but you may want to use it for consistency.
  2. Always try to use ‘var’ to declare variables in local functions. It makes sure you’re using a local copy of the variable instead of another variable of the same name in a different scope.

For example, the two similar functions here have very different effects:

var myvar = 0;
function affectsGlobalVar(i){
   myvar = i;
}
function doesNotAffectGlobalVar(i){
   var myvar = i;
}
like image 42
Matthew Vines Avatar answered Oct 01 '22 16:10

Matthew Vines


Without var a variable is defined as global.

like image 31
MatTheCat Avatar answered Oct 03 '22 16:10

MatTheCat


When adding Google AdSense to your page, you're linking a number of javascript files. The variables are defined in there.

http://pagead2.googlesyndication.com/pagead/show_ads.js should be one of the script references in your page.

like image 22
Babak Naffas Avatar answered Oct 04 '22 16:10

Babak Naffas


For the unenlightened like myself, JavaScript basically has two scopes for variables: global and local. Variables assigned outside of a function are global, and variables assigned inside of a function, using the var keyword, are local (not rocket surgery). However, if you leave the var keyword off, it assigns a global variable, regardless of where it’s declared.

From: http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript

like image 43
Gary Willoughby Avatar answered Oct 02 '22 16:10

Gary Willoughby