March 1999 Draft
JavaScript 2.0
Variables
previousupnext

Monday, March 22, 1999

Definitions

The general syntax for defining variables is:

VariableDefinition 
   [Visibilityvar [TypeExpressionIdentifier [= AssignmentExpression, ... , [TypeExpressionIdentifier [= AssignmentExpression;
|  [Visibilityconst [TypeExpressionIdentifier = AssignmentExpression , ... , [TypeExpressionIdentifier = AssignmentExpression ;

A variable defined with var can be modified, while one defined with const cannot. Identifier is the name of the variable and TypeExpression is its type. Identifier can be any non-reserved identifier. TypeExpression is evaluated at the time the variable definition is evaluated and should evaluate to a type t.

If provided, AssignmentExpression gives the variable's initial value v. If not, undefined is assumed; an error occurs if undefined cannot be coerced to type t. AssignmentExpression is evaluated just after the TypeExpression is evaluated. The value v is then coerced to the variable's type t and stored in the variable. If the variable is defined using var, any values subsequently assigned to the variable are also coerced to type t at the time of each such assignment.

Multiple variables separated by commas can be defined in the same VariableDefinition. The values of earlier variables are available in the TypeExpressions and AssignmentExpressions of later variables.

If omitted, TypeExpression defaults to type any for the first Identifier being declared and to the previous Identifier's TypeExpression value for each subsequent Identifier (the previous Identifier's TypeExpression is not evaluated twice; only its value is reused). Thus, the definition

var a, b=3, integer c=7, d, type e=boolean, number f, e g, int h;

is equivalent to:

var Any a=undefined;
var Any b=3;
var integer c=7;
var integer d=undefined;  // coerced to +0
var type e=boolean;
var number f=undefined;   // coerced to +0
var boolean g=undefined;  // coerced to false
var int h=undefined;      // coerced to int(0)

If Visibility is absent, a VariableDefinition defines local variables within the current Block scope, or class variables if the current Block scope is a ClassDefinition's Block according to the declaration scope rules. If Visibility is present, a VariableDefinition defines either global variables (if outside a ClassDefinition's Block) or class variables (if inside a ClassDefinition's Block). Unlike C++ or Java, JavaScript 2.0 does not use the static keyword to indicate class variables; instead, instance variables (i.e. non-static variables) are defined using the field keyword.

const Definitions

const means that Identifier cannot be written after it is defined. It does not mean that Identifier will have the same value the next time it is bound. For example, the following is legal; a new j binding is created each time through the loop:

var k = 0;
for (var i = 0; i < 10; i++) {
  const j = i;
  k += j;
}

If we collapse all block scopes inside a function, this example would no longer work.


Waldemar Horwat
Last modified Monday, March 22, 1999
previousupnext