Introduction to JavaScript Tutorial
By Neil Williams2007-11-09
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
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:
6.02e23
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);
alert(a);
-->
</script>
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.
Booleans
Use the keywords true and false to set variables as boolean data type.var bool = true;
Strings
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.
| Escape | Represents: |
|---|---|
\b | Backspace |
\f | Form Feed |
\n | Newline |
\r | Carriage Return |
\t | Tab |
\' | Apostrophe or single quote that does not terminate the string. |
\" | Double quote that does not terminate the string. |
\\ | Single backslash character |
\ddd | Character with Latin-1 encoding specified by three octal digits |
\xdd | Character with Latin-1 encoding specified by two hexadecimal digits |
\udddd | Character with Unicode encoding specified by four hexadecimal digits |
Objects
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;
Object properties can be accessed using standard array notation:
obj.y = 2;
obj["x"] = 1;
Typically, you create an object that is a member of a class of existing
objects and have suitable properties (and methods) already defined.
obj["y"] = 2;
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);
Tutorial Pages:
» It's flavours and versions
» Structure and syntax
» Data types and objects
» Functions and operators
» JavaScript Statements A-F
» JavaScript Statements G-Z
» JavaScript Events
» JavaScript Global Properties
» Javascript code to identify your browser
» Redirecting the browser once identified
Copyright © Neil Williams
