The var Statement



Navigation Aids -- This Page           Navigation Aids -- This Topic           Navigation Aids -- This Site




Review of Terms Related to This Topic





Definition and Purpose

A variable allocates storage space for new data. A variable has a name. The variable names are called identifiers. In JavaScript a variable is declared with the var keyword. Variables may be assigned initial values when declared. All variables have a data type that indicates what kind of data the variable holds.





Statement Syntax


JavaScript Statement Syntax
Statement Name ECMA Syntax
Full Syntax with Options Descriptive Links
Footnotes: Green background highlights the statement keywords.
Yellow background highlights the optional portion of a statement.
var 12.2 var identifier_1 [ = value_1]
[ ..., identifier_n [ = value_n]]
;
var identifier_1 [ = value_1]
[ ..., identifier_n [ = value_n]];




The Basics of the var Statement





Variable Declaration





Implicit Declaration

It is perfectly permissible to declare a variable without the var keyword. In this case, JavaScript will implicitly declare the variable for you. In addition, the variable is automatically given a "global" scope within the current document. To keep a variable's scope "local" to a function, you must declare the variable with the var keyword inside the function's braces.

The implicit declaration behaves much like a simple assignment operation. The simple assignment operator will assign the value on the right to the variable on the left.

Scripts written with implicit variable declarations are harder to read than those scripts that use explicit variable declarations. In addition, implicit declarations can lead to subtle errors due to variable scope (thinking its local when it is really global).





Variable Naming Conventions





Variables and Properties

The properties of objects and JavaScript variables have many similarities. And because they behave so similar, they are fundamentally the same.





ECMA-262 Section 12.2

The var statement is an ECMAScript core statement and is detailed is section 12.2 of the ECMAScript Language Specification. Here is a partial extraction of text from this section.

If the variable statement occurs inside a function declaration, the variables are defined with function-local scope in that function as described in 10.1.3. Otherwise, they are defined with global scope (that is, they are created as member of the global object as described in 10.1.3) using property attributes. Variables are created when the execution scope is entered. A block does not define a new execution scope. Only program and function declaration produce a new scope. Variables are initiated to undefined when created. A variable with an assignment expression is assigned its assignment expression when the variable statement is executed, not when the variable is entered.





Some Script Examples


Feature Script Giving
Declare and initialize an integer var x = 4;
alert(x);
4
Declare and initialize an float var x = 4.98765;
alert(x);
4.98765
Declare and initialize a string var x = "ABC";
alert(x);
ABC
Declare and initialize to null var x = null;
alert(x);
null
Declare and initialize to undefined var x = undefined;
alert(x);
undefined
Declare only var x;
alert(x);
undefined
Declare and initialize an integer and reassign to string var x = 4;
x = "ABC";
alert(x);
ABC
Implicit declaration; will have a global scope x = "EDF";
alert(x);
EDF
Legal variable names var _too;
var $too;
var too222;
var $222;
var $too$222;
Illegal variable names var 2too;
var *too;


Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson