Rx4AJAX   (Prescription for AJAX)

ECMA-262/Core JavaScript

ECMA-262/Core JavaScript





ECMA-262 Core: An Overview of JavaScript Data Types


Summary

The ECMA-262 standard defines the term type as a set of data values.

A fundamental characteristic of a programming language is the set of data types the language supports. The set of data types will govern the types of values (data) that can be represented within the language. Core ECMAScript provides JavaScript with two general kinds of data types. There are five primitive types and three reference types.

Values given to variables (and properties) will reflect a data type. Data types in JavaScript are not explicitly specified, thus, JavaScript is an untyped language. The data type of a JavaScript variable is derived and applied dynamically. The dynamic typing of a variable's data type is termed weak typing.

Partially due to weak typing, the complete set of data types in JavaScript is much smaller than other languages such as Java. In Java, the data type of variables are explicitly declared with numerous data type possibilities. The discussion of data types is closely related and overlap with the following topics:

Weak Typing of Variables

The JavaScript var statement is the means for which data is assigned to a variable. The JavaScript var statement provides no way to explicitly specify a type to the assigned data. The interpreter determines the data type by interrogating the token you specified as the initializing value. This process is called "weak typing". Weak typing of untyped variables is sometimes called dynamic typing and loose typing. Our link to Variable Typing explains weak typing and the process of dynamically deriving the data type by inspecting the value tokens.

The typeof Operator

JavaScript features an operator that will return the data type of a variable. The typeof operator will return a string with the type of data found in the operand. The single operand can be a variable of any type. var test = "test typeof" // assign a string value to test alert(typeof test); //the data type of var test is "string"
For a complete set of typeof results, view our page entitled "Unary Keyword Operator Group" and the section for the typeof operator.

Primitive Data Types

JavaScript supports five primitive data types: string, number, boolean, null and undefined. The primitive data types are the basic native data structures that describe data in storage. They also provide the building blocks from which more complex data structures can be built.

All five primitive data types will be allocated a location in storage. But what the storage contains can be tricky. The string, number and boolean data types are straight forward. There associated storage location will contain the actual value token used to initialize the variable. The "undefined" data type is a variable that has been declared but not yet initialized. It's allocated storage location is unaltered. The Null data type is a variable that has been declared and initialized to null. The allocated storage location is set to "empty".

The five primitive types are all covered separately on individual pages. Here are the links:

Reference Data Types

JavaScript supports three reference types: object, array and function. Unlike primitive types, the reference types have inconsistent storage size characteristics. An object can have any length with any number of methods and properties. An array can have any number of elements and a function can contain any amount of JavaScript code.

The reference data types are sometimes referred to as composite types. They can represent a collection of values that can hold any of the primitive types plus other objects.

The key to understanding reference types comes with a review of how variables are handled in memory. Following is a comparison of both reference types and primitive types:

  1. The size of reference type variables will vary, whereas primitive types all fit within eight bytes.
  2. Because of the varying variable size, values of variables for reference types are kept in the slower part of the memory called the "heap". The fixed length primitive type are kept in the faster memory called the "stack".
  3. To access the actual data or value stored in the heap, all reference types have a pointer on the stack that points to the location in the heap.
  4. Access to the values of primitive types is much faster, since:
    • The actual variable value exists in the faster stack.
    • There is only one memory call.
  5. The access to the values of reference types is much slower, since a call to the stack is required to get the pointer address for the actual data location in the heap. Once the pointer is fetched from the stack, then a memory call is required to the heap to retrieve the actual data.

To summarize, reference types require extra memory management because of there varying size. The extra step with the pointer (or reference) to the actual data is part of what make reference types unique from primitive types. Also refer to our page entitled "Primitive and Reference Values in Memory".

The three reference types are all covered separately on individual pages. Here are the links:

Type Conversions

JavaScript features many methods for converting from one data type to another. Type conversions can also occur automatically. We have built a separate page that highlights the type conversion options.

Top            


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