Data types and objects

Data Types
Javascript can handle basic data types like numbers (all treated as 64-bit floating point values), boolean values (true or false) and strings. Complex or compound data types can also be handled as objects or arrays. Objects are not C++/Java classes, they act as associative arrays with arbitrary data values and arbitrary names. Javascript 1.2 added support for regular expressions as a specialised data type.

Numbers in Javascript are all floating point values - whole numbers (integers) are not a separate type. Very large or very small values use an optional exponent syntax:
Hexadecimal or octal notation can be used to refer to an integer value, but Javascript will convert to a decimal floating point value:
Octal: 0377 = 255
Hexadecimal: 0xFF = 255
Number overflows will generate a special value that represents positive or negative infinity. This code illustrates the return values:

<script language="Javascript" type="text/javascript">
var x = -1000;
var y = 1000;
var a = Math.pow(x,y);

Test here

Number underflows will return a zero value. Illegal number operations like the square root of negative one or division by zero will yield an error, returning a special value NaN. Use the global isNaN(); to test for these values.

Use the keywords true and false to set variables as boolean data type.
var bool = true;

Not all character sets are supported by all browsers, although ECMA-262 requires Javascript to support the full 16-bit Unicode set. If you need to use Unicode, check browser compatibility before implementing the Javascript.
Special characters can be "escaped" to change the meaning of the character within the string.

\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
\xddCharacter with Latin-1 encoding specified by two hexadecimal digits
\uddddCharacter with Unicode encoding specified by four hexadecimal digits

An object can contain any number of properties of any type. Use the . operator to access a named property of an object. (In the code above, Math.pow() is used to access an object method.)
obj.x = 1;
obj.y = 2;
Object properties can be accessed using standard array notation:
obj["x"] = 1;
obj["y"] = 2;
Typically, you create an object that is a member of a class of existing objects and have suitable properties (and methods) already defined.
var time_now = new Date();

Javascript arrays are a form of object that uses numerical values for property names - as the object is normally an associative array anyway, this gives you a normal array, just like in C, PHP Java etc. To create an array, just use numerical values for the property names:
a[0] = 1;
a[1] = a[0] *2;
Every array has a length property assigned by Javascript. The last element in array is array[array.length-1];
Initialise the array using a constructor:
var x = new Array();
var y = new Array(4,5,7,12);

This is part of Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.