• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

11.5. Data Types

JavaScript supports three primitive data types: numbers, boolean values, and strings. In addition, it supports two compound data types: object and arrays. Functions are also a first-class data type in JavaScript, and JavaScript 1.2 adds support for regular expressions (described later) as a specialized type of object.


Numbers in JavaScript are represented in 64-bit floating-point format. JavaScript makes no distinction between integers and floating-point numbers. Numeric literals appear in JavaScript programs using the usual syntax: a sequence of digits, with an optional decimal point and an optional exponent. For example:


Integers may also appear in octal or hexadecimal notation. An octal literal begins with 0, and a hexadecimal literal begins with 0x:

0377 // The number 255 in octal
0xFF // The number 255 in hexadecimal

When a numeric operation overflows, it returns a special value that represents positive or negative infinity. When an operation underflows, it returns zero. When an operation such as taking the square root of a negative number yields an error or meaningless result, it returns the special value NaN, which represents a value that is not-a-number. Use the global function isNaN() to test for this value. The Number object defines useful numeric constants. The Math object defines various mathematical operations.


The boolean type has two possible values, represented by the JavaScript keywords true and false. These values represent truth or falsehood, on or off, yes or no, or anything else that can be represented with one bit of information.


A JavaScript string is a sequence of arbitrary letters, digits, and other characters. The ECMA-262 standard requires JavaScript to support the full 16-bit Unicode character set. Internet Explorer 4 supports Unicode, but Navigator 4 supports only the Latin-1 character set. String literals appear in JavaScript programs between single or double quotes. One style of quotes may be nested within the other:

"Wouldn't you prefer O'Reilly's book?"

When the backslash character (/) appears within a string literal, it changes or "escapes" the meaning of the character that follows it. The following table lists these special escape sequences:

\fForm feed
\;rCarriage return
\'Apostrophe or single quote that does not terminate the string
\"Double quote that does not terminate the string
\\Single backslash character
\dddCharacter with Latin-1 encoding specified by three octal digits ddd
\xddCharacter with Latin-1 encoding specified by two hexadecimal digits dd
\uddddCharacter with Unicode encoding specified by four hexadecimal digits dddd
\nn, where n is any character other than those shown above

The String class defines many methods you can use to operate on strings. It also defines the length property, which specifies the number of characters in a string. The addition (+) operator concatenates strings. The equality (==) operator compares two strings to see if they contain exactly the same sequences of characters. (This is compare-by-value, not compare-by-reference, as C, C++, or Java programmers might expect.) The inequality operator (!=) does the reverse. The relational operators (<, <=, >, and >=) compare strings using alphabetical order. JavaScript strings are immutable, which means there is no way to change the contents of a string. Methods that operate on strings typically return a modified copy of the string.


An object is a compound data type that contains any number of properties. Each property has a name and a value. The . operator is used to access a named property of an object. For example, you can read and write property values of an object o as follows:

o.x = 1;
o.y = 2;
o.total = o.x + o.y;

Object properties are not defined in advance as they are in C, C++, or Java; any object can be assigned any property. JavaScript objects are associative arrays: they associate arbitrary data values with arbitrary names. Because of this fact, object properties can also be accessed using array notation:

o["x"] = 1;
o["y"] = 2;

Objects are created with the new operator. You can create a new object with no properties as follows:

var o = new Object();

Typically, however, you use predefined constructors to create objects that are members of a class of objects and have suitable properties and methods automatically defined. For example, you can create a Date object that represents the current time with:

var now = new Date();

You can also define your own object classes and corresponding constructors. In JavaScript 1.2, you can use object literal syntax to include objects literally in a program. An object literal is a comma-separated list of name/value pairs, contained within curly braces. For example:

var o = {x:1, y:2, total:3};


An array is a type of object that contains numbered values rather than named values. The [] operator is used to access the numbered values of an array:

a[0] = 1;
a[1] = a[0] + a[0];

The first element of a JavaScript array is element 0. Every array has a length property that specifies the number of elements in the array. The last element of an array is element length-1. You create an array with the Array() constructor:

var a = new Array();	// Empty array
var b = new Array(10);	// 10 elements
var c = new Array(1,2,3);	// Elements 1,2,3

In JavaScript 1.2, you can use array literal syntax to include arrays directly in a program. An array literal is a comma-separated list of values enclosed within square brackets. For example:

var a = [1,2,3];
var b = [1, true, [1,2], {x:1, y:2}, "Hello"];

The Array class defines a number of useful methods for working with arrays.

Functions and methods

A function is a piece of JavaScript code that is defined once and can be executed multiple times by a program. A function definition looks like this:

function sum(x, y) {
  return x + y;

Functions are invoked using the () operator and passing a list of argument values:

var total = sum(1,2);  // Total is now 3

In JavaScript 1.1, you can create functions using the Function() constructor:

var sum = new Function("x", "y", "return x+y;");

In JavaScript 1.2, you can define functions using function literal syntax:

var sum = function(x,y) { return x+y; }

When a function is assigned to a property of an object, it is called a method of that object. Within the body of the function, the keyword this refers to is the object for which the function is a property. Within the body of a function, the arguments[] array contains the complete set of arguments passed to the function. The Function and Arguments classes represent functions and their arguments.

null and undefined

The JavaScript keyword null is a special value that indicates "no value". If a variable contains null, you know that it does not contain a valid value of any type. There is one other special value in JavaScript: the undefined value. This is the value returned when you use an undeclared or uninitialized variable or when you use a nonexistent object property. There is no JavaScript keyword for this value.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint