Register for EarthWeb's Million Dollar Sweepstakes!
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



CAUTION:  

Because JavaScript is still being developed and refined, the list of reserved keywords might change or grow over time. Whenever a new version of JavaScript is released, it might be a good idea to look over its new capabilities with an eye toward conflicts with your JavaScript programs.


Using JavaScript Statements

This section provides a quick reference to some of the more important JavaScript statements. Those listed here are in alphabetical order—many have examples. The formatting of these entries means the following:

  All JavaScript keywords are in monospaced font.
  Words in monospace italic represent user-defined names or statements.
  Any portions enclosed in square brackets ([ and ]) are optional.
  {statements} indicates a block of statements, which can consist of a single statement or multiple statements enclosed by braces.

The break Statement The break statement terminates the current while or for loop and transfers program control to the statement that follows the terminated loop.

Syntax

break

Example

The following function scans the list of URLs in the current document and stops when it has seen all URLs or when it finds a URL that matches the input parameter searchName:

function findURL(searchName) {
   var i = 0;
   for (i=0; i < document.links.length; i++) {
      if (document.links[i] == searchName) {
         document.writeln(document.links[i] + “<br>”)
         break;
      }
   }
} 

The continue Statement The continue statement stops executing the statements in a while or for loop and skips to the next iteration of the loop. It doesn’t stop the loop altogether like the break statement; instead, in a while loop, it jumps back to the condition. In a for loop, it jumps to the update expression.

Syntax

continue

Example

The following function prints the odd numbers between 1 and x; it has a continue statement that goes to the next iteration when i is even:

function printOddNumbers(x) {
   var i = 0
   while (i < x) {
      i++;
      if ((i % 2) == 0) // the % operator divides & returns the remainder
         continue
      else
         document.write(i, “\n”)
   }
} 

The for Loop A for loop consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop. These parts do the following:

  The starting expression, initial_expr, is evaluated before the loop starts. It is most often used to initialize loop counter variables. You are free to use the var keyword here to declare new variables.
  A condition is evaluated on each pass through the loop. If the condition evaluates to TRUE, the statements in the loop body are executed. You can leave the condition out. If you do, it always evaluates to TRUE. If you leave the condition out, make sure to use break in your loop when it is time to exit.
  An update expression, update_expr, is usually used to update or increment the counter variable or other variables used in the condition. This expression is optional; you can update variables as needed within the body of the loop if you prefer.
  A block of statements is executed as long as the condition is TRUE. This block can have one or multiple statements in it.

Syntax

for ([initial_expr;] [condition;] [update_expr]) {
   statements
}

Example

This simple for statement prints out the numerals from 0 to 9. It starts by declaring a loop counter variable, i, and initializing it to 0. As long as i is less than 9, the update expression increments i, and the statements in the loop body are executed.

for (var i = 0; i <= 9; i++) {
   document.write(i);
} 

The for…in Loop The for…in loop is a special form of the for loop that iterates the variable variable-name over all the properties of the object named object-name. For each distinct property, it executes the statements in the loop body.

Syntax

for (var in obj) {
   statements
}

Example

The following function takes as its arguments an object and the object’s name. It then uses the forin loop to iterate through all the object’s properties and writes them into the current Web page.

function dump_props(obj,obj_name) {
   for (i in obj)
      document.writeln(obj_name + “.” + i + “ = “ + obj[i] + “<br>”);
} 

The function Statement The function statement declares a JavaScript function; the function may optionally accept one or more parameters. To return a value, the function must have a return statement that specifies the value to return. All parameters are passed to functions by value—the function gets the value of the parameter but cannot change the original value in the caller.

Syntax

function name([param] [, param] […, param]) {
   statements
}

Example

This example defines a function called PageNameMatches, which returns TRUE if the string argument passed to the function is the title of the current document.

function PageNameMatches(theString) {
   return (document.title == theString)
} 

The if…else Statement The if…else statement is a conditional statement that executes the statements in block1 if condition is TRUE. In the optional else clause, it executes the statements in block2 if condition is FALSE. The blocks of statements can contain any JavaScript statements, including further nested if statements.

Syntax

if (condition) {
    statements
}
[else {
    statements
}]

Example

This if...else statement calls the Message.Decrypt() method if the Message.IsEncrypted() method returns TRUE and calls the Message.Display() method otherwise.

if (Message.IsEncrypted()) {
    Message.Decrypt(SecretKey);
}
else {
    Message.Display();
} 


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.