ECMA-262 Core: JavaScript Variables



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








Introduction And Review

A variable associates a name (sometimes called an identifier) with a memory location. A value in turn is stored as a data object in that location so that the object can be accessed later via the variable name.

Variables are declared with the var statement. Data objects are assigned to variables with the assignment operator. A variable is initialized to a value with the assignment of a data object that is normally expressed as a literal. All variables that have been declared and initialized will have the following properties:

The data type determines storage requirements (both size and location) and range of values. A discussion on variables must also include these properties. A data type is intrinsic to the idea of a variable.

The remainder of this page will digress on these properties. Most of these topics have been covered extensively in other sections of our site. We provide a summary here and then link to the detail on a separate page.

Identifying Name

Identifiers are lexical tokens that name entities. The concept is analogous to that of a "name". Naming entities makes it possible to refer to them. Objects, functions, variables and statements (label name) may all be named with an identifier.

Here are the rules for identifiers.

Data Type

Every variable that has been initialized will have a data type. JavaScript has two general kinds of data types. There are five primitive types and three reference types.

Most computer languages such as Java are strongly typed. That is, the data type must be explicitly specified at the time the variable is declared. JavaScript is weakly typed. Weakly typed variables are implicitly typed. That is, the programmer does not explicitly provide the data type, but the interpreter determines the data type from the value token given with the assignment statement. The interpreter looks at the value token for certain syntactical constructs to identify reference types. And for primitive data types, it looks at value ranges that match the predefined value ranges given for each primitive data type.

The data type of a variable has the following properties:

Data types are described on our site at page entitled, "ECMA-262 Core: An Overview of JavaScript Data Types".

Storage Size

Each primitive data type has a fixed data size. Variables of these data types take on the same storage requirements.

The floating point number has the largest requirement for storage. It takes 8-bytes to store the 64-bit float. Therefore, memory is allocated in 8-byte clusters for all primitive typed variables even though some variables don't need the full 8-bytes.

The 8-byte allocation for all primitive variables is mandated by two JavaScript featues. One is weak typing. The other is type conversion. As of the result of these two features, a variable can have it's data type dynamically and explicitly altered. A variable that was a float can be reinitialized as a integer which can be reinitialized as a boolean and so on. Given the 8-byte memory allocation requirement, the address need never change for a variable of a primitive data type even though data types and values may change. Reference our page on type conversions.

The storage size (or memory allocation) for a variable of a reference type is not fixed. It will vary depending on the nature of the reference variable:

Also refer to our page with the section entitled "Reference Data Types".

Storage Location

Variables of primitive data types will reside in the faster part of memory because of there small memory allocation requirements and there fixed memory size. Reference data type will reside in the slower part of memory.

If you are interested on why, how and where primitive and reference type will reside in memory, see our page on: "Primitive and Reference Values in Memory".

Range of Values

We have mentioned that JavaScript determines data types dynamically by interpreting the value token (literal) used to initialize the variable. We have identified these range of values for each of the primitive data types on a separate page entiltled "ECMA-262 Core: JavaScript Literals".

Scope

A JavaScript variable can have either local or global scope. Global scope means the variable can be seen and accessed from anywhere in the program and does not belong to any one function. Local scope means the variable was declared in some local context such as a function. Local variables are accessible only from the function or block in which it is declared. This topic is covered in detail on the page entitled "Variable Scope".



Top            

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