Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Testing Conditions JavaScript provides a single type of control statement for making decisions: the if…else statement. To make a decision, you supply an expression that evaluates to TRUE or FALSE; which code is executed depends on what your expression evaluates to.

The simplest form of if…else uses only the if part. If the specified condition is TRUE, the code following the condition is executed; if not, it’s skipped. In the following code fragment, for example, the message appears only if the condition (that the lastModified.year property of the document object says it was modified before 1995) is TRUE:

if (document.lastModified.year < 1995)
   document.write(“Danger! This is a mighty old document.”)

You can use any expression as the condition. Because you can nest expressions and combine them with the logical operators, your tests can be pretty sophisticated. For example:

if ((document.lastModified.year >= 1995) && (document.lastModified.month >= 10))
   document.write(“This document is reasonably current.”)

The else clause enables you to specify a set of statements to execute when the condition is FALSE. For instance,

if ((document.lastModified.year >= 1995) && (document.lastModified.month >= 10))
   document.write(“This document is reasonably current.”)
else
   document.write(“This document is quite old.”) 

Repeating Actions JavaScript provides two loop constructs that you can use to repeat a set of operations. The first, called a for loop, executes a set of statements some number of times. You specify three expressions: an initial expression that sets the values of any variables you need to use, a condition that tells the loop how to see when it is done, and an increment expression that modifies any variables that need it. Here’s a simple example:

for (count=0; count < 100; count++)
   document.write(“Count is “, count);

This loop executes 100 times and prints out a number each time. The initial expression sets the counter, count, to zero. The condition tests to see whether count is less than 100 and the increment expression increments count.

You can use several statements for any of these expressions, as follows:

for (count=0, numFound = 0; (count < 100) && (numFound < 3); count++)
   if (someObject.found()) numFound++;

This loop either loops 100 times or as many times as it takes to “find” three items—the loop condition terminates when count >= 100 or when numFound >= 3.

The second form of loop is the while loop. It executes statements as long as its condition is TRUE. You can rewrite the first for loop in the preceding example, for instance, as follows:

count = 0
while (count < 100) {
   count++;
   if (someObject.found()) numFound++;
   document.write(“Count is “, count)
}

Which form you use depends on what you are doing; for loops are useful when you want to perform an action a set number of times, and while loops are best when you want to keep doing something as long as a particular condition remains TRUE. Notice that by using braces, you can include more than one command to be executed by the while loop. (This is also true of for loops and if...else constructs.)

JavaScript Reserved Words

JavaScript reserves some keywords for its own use. You cannot define your own methods or properties with the same name as any of these keywords; if you do, the JavaScript interpreter complains.


Some of these keywords are reserved for future use. JavaScript might enable you to use them, but your scripts may break in the future if you do.

Table 18.3 shows some of JavaScript’s reserved keywords.

Table 18.3 JavaScript Reserved Keywords Should Not Be Used in Your JavaScripts

abstract double instanceof super
boolean else int switch
break extends interface synchronized
byte FALSE long this
case final native throw
catch finally new throws
char float null transient
class for package TRUE
const function private try
continue goto protected var
default if public void
do implements return while
import short with in
static


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.