[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

javascript main
1. javascript basics
2. core javascript
a. js syntax
* b. js data types
c. js variables
d. js expressions
e. js operators
3. js statements
4. js functions
5. js arrays
6. js objects
7. debugging js
8. js client side
9. the js bom
10. js frames and windows
11. js forms
12. js regexp
13. js cookies
14. basic dhtml


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Data Types

These tutorials are about JavaScript and its use for client-side Web programming.

Data Types

data type: a general term to describe categories of values that a program or programming language can make use of

A computer works with values. It manipulates values and produces some kind of result. Depending on the program, it may be limited as to what types of values it can work with. The types of values a program can work with are known as its data types.

JavaScript supports many types of data.

Data types can be broken into two broad categories, primitive and composite.

A primitive data type is a data type that stores a single value, normally a literal of some sort, such as numbers and strings.

A composite data type, for the purposes of JavaScript, is the same thing as an object. It is a data type that can consist of multiple values grouped together in some way. JavaScript treats objects as associative arrays. An associative array is an array that can have its elements associated with names in addition to their numeric index position in the array.

JavaScript also treats functions as objects. This means that functions can be used just like any other variable.

Primitive Data Types

There are three primitive data type of interest to us:

  • numbers
  • strings
  • boolean values

Numbers

Numbers are the easist of the data types to understand. They represent numeric values.

The simplest type of number is an integer.

An integer is a base-10 whole number. In other words, it is a number with no fractional component. JavaScript can handle integer values between 253 and -253. Some JavaScript functions, however, cannot handle extremely large or small numbers, so it is probably a good idea to try to keep your numbers in the range of 231 and -231. Since that is plus or minus 2 trillion, this shouldn't be a problem as long as you are not trying to track the national debt.

All of the following are valid integers.

  • 0
  • -75
  • 10000000
  • 2

Another commonly used type of number is a floating-point number. A floating-point number is a number that is assumed to have a decimal point. The decimal point is the point that floats and gives the data type its name.

Floating-point numbers have a fractional component, even if that fractional component is zero. They can be represented as a real number (integer - decimal point - fractional value) such as in 3.1714, or using exponential notation.

Exponential notation is a number multiplied by ten times the power of some exponent, for instance 3.1714x10-15 (0.0000000000000031714). Exponential notation is usually written with an "e" instead of the "x10", so that JavaScript will know it is a floating-point number and not an equation. This would read as 3.1714e-15.

All of the following are valid floating-point numbers.

  • 5.00
  • 1.3333333333333
  • 1.0e25
  • 7.5e-3

JavaScript stores floating-point numbers in exponential form, storing the number and its exponent. Just to confuse the issue, it also stores integers as floating-point numbers, but this is transparent to you as the programmer and you don't have to worry about it.

Being a computing language, JavaScript also has the ability to handle hexadecimal (four bit) and octal (three bit) numbers. Octal support is newer and is best avoided since older version of JavaScript will think the octal number is a hexadecimal number. All hexadecimal numbers begin with a zero and an X, for instance 0xff0000. Octal numbers, if they are supported, begin with a zero, for instance, 0755. If they are not supported, this notation will be treated as another way of expressing hexadecimal numbers.

JavaScript also has some numeric keyword literals you should know about. These are listed below.

Infinity
Infinity has the value of infinity, which is to say a number larger than the largest number that JavaScript can represent. Not really infinity, but from a computing perspective, it might as well be.
There is also the keyword literal -Infinity for the negative infinity.
As well as keywords, these can be represented as properties of the Number object as Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY.
Using the number object properties comes from older JavaScript, while the keyword literals were first implemented with JavaScript 1.3. You can test for infinity with the isFinite() function.
NaN
NaN is the value returned when you try to treat something that is not a number as a number. For instance the results of 7 times "abc" is not a number. The old form of it is Number.NaN. You can test for not-a-number values with the isNaN() function.
Number.MAX_VALUE
Number.MAX_VALUE represents the largest representable number for JavaScript
Number.MIN_VALUE
Number.MIN_VALUE represents the small representable non-zero number for JavaScript. In other words as close as JavaScript can get to zero with actually reaching zero.

Strings

A string is sequence of valid characters within a given character set. It is normally used to represent text. A string literal is defined by enclosing it in matching single or double quotes.

If you create a string with nothing in it, it is called an empty string. It is normally created by two quote marks with nothing between them.

Some characters that you may want in a string may not exist on the keyboard, or may be special characters that can't appear as themselves in a string. In order to put these characters in a string, you need to use an escape sequence to represent the character. An escape sequence is a character or numeric value representing a character that is preceeded by a backslash ( \ ) to indicate that it is a special character.

Some escaped characters are as follows:

Escape Sequence Character
\0 (blackslash zero) The Null character (\u0000). If you've taken COBOL, you may recognize this as COBOL's low-value, used to populate fields with a clearly defined invalid value that tests less than anything else.
\b Backspace.
\t Tab. Tabs behave erratically on the Web and are best avoided, but sometimes you need them.
\n New line (\u0000a). Inserts a line break at the point specified. It is a combination of the carriage return (\r) and the form feed (\f).
\" Double quote.
\' Single quote, or an apostrophe, such as in can\'t.
\\ The backslash, since by itself it is a command.
\x99 A two digit number specifying the hexadecimal value of a character in the Latin-1 character set (ASCII).
\u9999 A four digit hecadecimal number specifying a character in the Unicode character set. This is not supported before JavaScript 1.3.

Boolean Values

There are two boolean values, true and false. These are normally the result of a logical comparison in your code that returns a true/false or yes/no result, such as
a == b.
This statment reads: does the value of variable a equal the value of variable b?

If you need to use boolean values in computations, JavaScript will automatically convert true to 1 and false to 0.

The conversion can also work the other way. When testing for the result of a comparison, JavaScript will treat any non-zero value as true, and a zero value as false. JavaScript will also test to false when you test for the existence of something that does not exist.

Composite Data Types

All composite data types can be treated as objects, but we normally categorize them by their purpose as a data type. For composite data types we will look at objects, including some special pre-defined objects that JavaScript provides, as well as functions and arrays. We will look at all of these elements in more detail elsewhere, so we will only touch on them lightly here.

Objects

An object is a collection of named values, called the properties of that object. Functions associated with an object are referred to as the methods of that object.

Properties and methods of objects are referred to with a dot.notation that starts with the name of the object and ends with the name of the property. For instance
image.src.

Normally in objects there are only two nodes, the object and the property, but as we will discover, sometimes the properties can have properties of their own, creating an object tree which you have to specify the path through in the same way you would with a directory tree (except you are using periods instead of slashes).

For instance,
document.form1.namefield.

Objects in JavaScript can be treated as associative arrays. This means that image.src and image['src'] are equivalent. This flexibility has its uses, since this means that you can pass property names as the values of variables.

For instance, this code snippet:
img[iProp] = newImage[iProp];
might exist in a loop that resets the properties of an image one by one.

JavaScript has many predefined objects, such as a Date object and a Math object. These are used much as function libraries are used in a language like C. They contain a collection of useful methods that are predefined and ready for use in any JavaScript code you may write.

Functions

A function is a piece of code, predefined or written by the person creating the JavaScript, that is executed based on a call to it by name.

Any JavaScript that is not inside a function (or is not associated with some even attribute or hyperlink attribute) is executed the moment the Web browser reaches it when first parsing the document. Functions allow execution to be delayed. They also allow for the same piece of code to be re-used many times in the same document, since functions allow the section of code they contain to be referred to by name.

A function is a data type in JavaScript. This is different from many other programming languages. This means that they can be treated as containing values that can be changed.

Arrays

An Array is an ordered collection of data values.

In some programming languages, arrays have very specific limitations. In JavaScript, an array is just an object that has an index to refer to its contents. In other words, the fields in the array are numbered, and you can refer to the number position of the field.

The array index is included in square brackets immediately after the array name. In JavaScript, the array index starts with zero, so the first element in an array would be arrayName[0], and the third would be arrayName[2].

JavaScript does not have multi-dimensional arrays, but you can nest them, which is to say, an array element can contain another array. You access them listing the array numbers starting for the outmost array and working inward. Therefore, the third element (position 2) of or inside the ninth element (position 8) would be arrayName[8][2].

We will be discussing how to create and work with arrays elsewhere.

JavaScript Object Literals

JavaScript also has two keyword literals that it considers to be objects. These are null and undefined.

Something with the value null has no value, which is to say that it does not contain a valid object, array, string, number, or boolean value. It is the value of no value. Normally you assign the value null to something when you want it to have a valid but non-existent value.

Unlike null, undefined is not a keyword. It is a predefined global variable, but it is treated like any other variable. Be careful not to overwrite it. The variable undefined is used to store the value of an undefined variable.

An undefined variable is one that has been named, but does not have a value assigned to it. This means that it is not zero, it is not null, but it is undefined. The reason it is undefined is that even though there is officially no value in there, there is really whatever garbage is still sitting in that location in memory now contained in that variable (computers don't really bother to clean up after themselves when things are deleted, they just actively forget that anything is there). To avoid this problem, JavaScript assigns the value of undefined to a variable until a value is assigned to it.

[top]