Share via


var Statement (Windows Scripting - JScript)

 

Declares a variable.

Syntax

var variable1 [ = value1 ] [, variable2 [ = value2], ...] 

Arguments

  • variable1, variable2
    The names of the variables being declared.

  • value1, value2
    The initial value assigned to the variable.

Remarks

Use the var statement to declare variables. These variables can be assigned values at declaration or later in your script.

The first time a variable appears in your script constitutes its declaration.

You can declare a variable without using the var keyword and assign a value to it. This is known as an implicit declaration, and it is not recommended. An implicit declaration gives the variable global scope. When you declare a variable at the procedure level, though, you typically do not want it to have global scope. To avoid giving the variable global scope, you must use the var keyword in your variable declaration.

If you do not initialize your variable in the var statement, it is automatically assigned the JScript value undefined.

The following example illustrates the use of the var statement.

var index;
var name = "Thomas Jefferson";
var answer = 42, counter, numpages = 10;
var myarray = new Array();

Requirements

Version 1

Change History

Date

History

Reason

September 2009

Added information to Remarks.

Customer feedback.

March 2009

Added array declaration to the example.

Customer feedback.

See Also

function Statement (Windows Scripting - JScript)
new Operator (Windows Scripting - JScript)
Array Object (Windows Scripting - JScript)
JScript Variables (Windows Scripting - JScript)