CIW Course Revision Site
Click here for domain name registration and web hosting

JavaScript - Language Objects

CIW Website Design Manager Course Section 2, Part A1, Chapter 7

The Language objects are treated slightly differently to other JavaScript objects. If you examine the hierarchy model closely you will notice that Language object names all start with a capital letter. So we have: Array, Date, Math, String, Function, Boolean and Number. As you know, JavaScript is case-sensitive so this is important to be aware of.

String Object

A String is a series of characters, generally letters but it can contain any characters. There is one special character that cannot appear in a string, as it stands, and this is "\", the backslash, which is probably more correctly referred to, in this context, as the ESC character. This is used to create escape sequences to represent special characters in a string.

JavaScript Escape Sequences
Characters Represents
\b Backspace
\f Form feed
\n New line
\t Tab
\r Carriage return
\" Double quote
\' Single quote
\\ Backslash

For example: document.write("<TITLE>My Home Page</TITLE>\n"); would write the line, but ends it with the new line character.

String methods, and other language methods, are subordinates of the String object, so they employ the same dot notation to invoke them. If variable strVar contains a string then strVar.bold()  will make it appear as bold text. What it actually does, is wrap the string in the <B> and </B> tags. String methods can be applied equally to a string variable or to a literal string.

There are a large number of String methods, here are just a few that are, perhaps, more commonplace:

String Object Methods
Method Description
link("URL") Uses the String value as the link text to URL
fontcolor("color") Sets the text colour of the string
bold() uses bold face
toUpperCase() converts string to upper case characters
length returns the number of characters in String

String Evaluation Methods

I think that this paragraph may have been better titled as string manipulation.

We shall look at finding specific characters within a string and extracting portions of a string.

The method "length" returns the number of characters in the string. indexOf(SearchText, StartPosn) finds the first occurrence of SearchText in the string. lastIndexOf(SearchText, EndPosn) finds the last occurrence of SearchText in the string. lastIndexOf uses an EndPosn as it is searching the string in reverse.

substring(StartPosn, EndPosn) returns a portion of the string. It should be remembered that the numbering of character positions in the string begins at 0 (zero). charAt(Posn) returns a single character.

Array Object

An Array can be thought of as a collection of variables that can be manipulated as one. JavaScript arrays are zero-based which means the first element number is 0.

var season = new Array();
season[0] = "Spring";
season[1] = "Summer";
season[2] = "Autumn";
season[3] = "Winter";

Note the use of the uppercase "A" in the array declaration. You may recall that all language objects begin with an uppercase letter. The length property of the array returns the number of elements in the array.

Date Object

The Date object holds date and time information and allows this to be manipulated. There are a large number of built-in methods for the Date object, below is but a small sample:

Date Object Methods
Method Description
getDate() Returns the day number. 1 to 31
setDate(value) Sets the day number
getHours() Returns the hour number. 0 to 23
setHours(value) Set the Hour number

The Date object also has a timer method allowing a function to be called repeatedly at a defined interval. The setTimeout and clearTimeout methods are used to control this interval.

Math Object

The Math object permits the calculation of a number of mathematical formulae or functions. It has a number of properties and methods. These cover sine, cosine, tangent, etc. It will also round numbers up or down, and return values such as Pi.

Design by Stephen

Certified Internet Webmaster

Page last Edited: 10 Nov 2011