Variable Typing



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








Variable Typing

When you declare a variable, the initialized value will receive a data type. In some computer languages, the data type is explicitly specified at the time the variable is declared. Not so with JavaScript. The JavaScript language has no way of explicitly assigning a data type with the var statement. As far as the programmer is concerned, the variable is "untyped". So how does JavaScript assign a particular data type to a variable? The interpreter determines the data type by interrogating the literal token you specified as the initializing value. This process is called "weak typing".


Weak Typing

Weak typing of untyped variables is sometimes called dynamic typing and loose typing. As stated above, this process begins by looking at the value used to initialize the variable. The interpreter determines whether the initializing token is a primitive type or a reference type. A variable can hold one of these two general types of values: primitive values and reference values.


Primitive Values and Weak Typing

The interpreter dynamically determines the primitive data type by testing each value token for a predetermine range of values (and punctuation for the string type). The range of values for the five primitive data types are:



Reference Values and Weak Typing

JavaScript supports three reference values: objects arrays and functions. The interpreter knows it is dealing with a reference value when it encounters certain keywords or symbols in the token(s) used to initialize the variable.

The links to the literal definitions of the three reference types are given here:



typeof Operator

The typeof operator returns the data type of a variable. This operator adheres to similar logical criteria used by the interpreter when it performs dynamic typing. For more on the typeof operator.



String Data Type: Primitive or Reference??

The size of a string can vary from one string to another. In many languages, the varying size attribute of a string would qualify it for the reference type. However, ECMAScript classifies the string values as a primitive type even though the string behavior in memory management resembles that of a reference type.



Top            

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