Is var required when declaring Javascript variables?

Possible duplicate:
Javascript: uses 'var' to declare variables optional?

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

For example, instead of

var message = "Hello World!" 

Can i use

 message = "Hello World!" 

?

I noticed that scripts like Google Adsense do not use var

Example:

 google_ad_width = 160; google_ad_height = 600; google_color_border = "000000"; google_color_bg = "ffffff"; 
+59
javascript variables
Oct 08 2018-10-10
source share
6 answers

If you do not declare a variable (explicitly create it in the current scope) using var , let or const , then (in lax mode) you create an implicit global variable.

Global variables are a fantastic way to allow different functions to overwrite each other's variables (i.e. they make code difficult to maintain).

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

(The scope const and let and the variables for the current block instead of the function usually make managing variables even easier than var .)

Google Adsense uses global variables because it breaks scripts into two separate parts (one local and one remote). A cleaner approach would be to call the function defined in the remote script and pass parameters as arguments instead of calling them from the global scope.




Modern JS should be written in strict mode , which prohibits implicit global variables (preferring to explicitly declare them at the top level instead, thereby preventing random global variables from being sealed).

+105
Oct 08 2018-10-10
source share

Yes, you should always use var .

Not using var has two main disadvantages:

  • Access to a variable inside a function that is not defined inside this function will force the interpreter to search the chain of visibility for a variable with this name until it finds it or receives a global object (accessible in browsers through window ), where it creates a property. This global property is now universally available, potentially causing confusion and hard-to-detect errors;
  • Access to an undeclared variable will result in an ECMAScript 5 strict mode error.

Also, not using var for a global variable is not quite the same as using var : when using var property that it creates on the global object has an 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 
+10
Oct 08 '10 at 17:05
source share

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

  1. For global variables, this does not matter, but you can use it for consistency.
  2. Always try to use 'var to declare variables in local functions. This ensures that you use a local copy of the variable instead of another variable with the same name in a different scope.

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

 var myvar = 0; function affectsGlobalVar(i){ myvar = i; } function doesNotAffectGlobalVar(i){ var myvar = i; } 
+4
Oct 08 2018-10-10
source share

Without var, the variable is defined as global.

+3
Oct 08 2018-10-10
source share

When you add Google AdSense to your page, you link several javascript files. Variables are defined there.

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

+1
Oct 08 2018-10-10
source share

For an unenlightened person like me, JavaScript basically has two areas for variables: global and local. Variables assigned outside the function are global, and variables assigned inside the function using the var keyword are local (non-missile) surgery). However, if you leave var keyword off, it assigns a global variable, no matter where it is declared.

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

+1
Oct 08 2018-10-10
source share



All Articles