Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Understanding and using JavaScript statements

Moving through the eleven statements supported by JavaScript

  • Print
  • Feedback

Page 3 of 6

for (Count=1; Count<=10; Count++) {
    document.write ("Iteration: "+Count+"<BR>");
}


Count is the variable name used to store the for loop counter. The for loop starts out with 1 and proceeds to 10. The test expression is Count<=10, which reads:

Count is less than or equal to 10


As long as this expression is true, the for loop continues. Do note that the Increment argument is also an expression and in the example uses the Count variable to increment the for loop by 1 for each iteration. There's no law that says you must increment the for loop by ones. Here's just one of the many alternatives. This example counts by tens, from 10 to 100.

for (Count=1; Count<101; Count+=10) {
    document.write ("Iteration: "+Count+"<BR>");
}


Back to index

For...in

The for...in statement is a special version of the for statement described in the previous section. For...in is used to display the property names and/or property contents of objects. It is mostly handy as a debugging and testing tool: If a portion of your JavaScript code isn't working properly, and you suspect it may be the fault of a JavaScript object you are trying to use, you can examine all of the properties for that object with the for...in statement.

Unlike the for statement, for...in doesn't use incrementing tests or other expressions. You provide the name of a holding variable (the name of the variable is up to you) and the object you want to use. The basic syntax for the for...in statement is:

for (var in object) {
    statements
}


  • var is the name of a variable
  • object is the object you wish to examine
  • statements are one or more JavaScript instructions you wish to execute for each property returned by the for...in loop


As an example, suppose you want to determine the properties of the navigator object (this object contains details about the Netscape Navigator or other browser you are using). The following code displays each property name in an alert box. When running the script, click OK to proceed to the next property name. The loop automatically ends when there are no more properties in the object.

for (temp in navigator) {
    alert (temp);
}


A variation of this example is shown below. It not only displays the property names but also displays the contents of each property (some properties are empty and therefore no contents are shown for them). The contents of the properties are displayed by using the syntax object[var], or in this case navigator[temp].

for (temp in navigator) {
    alert (temp + ": " + navigator[temp]);
}


The for..in loop can be used for all object types. You can use it to iterate through all the properties for a form in a document, for example, or through the properties of a user-defined object. For instance, to cycle through all the properties of a form, you'd use the for...in loop as in the following example (assume the form name is "myform"):

<SCRIPT>
function test() {
    for (temp in document.myform) {
        alert (temp);
    }
}
</SCRIPT>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="box1"><BR>
<INPUT TYPE="text" NAME="box2"><BR>
<INPUT TYPE="text" NAME="box3"><P>
<INPUT TYPE="button" VALUE="Click" onClick="test()">
</FORM>


Back to index

Function

The function statement lets you create your own user-defined functions (as well as user-defined objects and methods for those objects). Functions are self-contained routines that can be "called" elsewhere within your JavaScript code. For example, if you have a function named writeMyName, which displays your name in headline text, you can activate it by merely referring to the name writeMyName someplace within your JavaScript code. Here's a short test that shows how this might work:

<HTML>
<HEAD>
<TITLE>Function Test</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function writeMyName () {
        MyName="John Doe"
        alert (MyName)
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE="button" NAME="button1" VALUE="Click Me!" onClick="writeMyName()"><P>
</FORM>
</BODY>
</HTML>


The writeMyName function is defined within <SCRIPT>...</SCRIPT> tags. It is activated (otherwise known as "called") when the form button is pushed. This calling action is accomplished using the onClick event handler, defined in the <INPUT> tag for the form button.

Back to index

If...else

The if, along with its optional else, statement is used to build an "if conditional" expression. It is called a conditional expression because it tests for a specific condition.

  • If the expression is true, the script performs the instructions following the if statement.
  • If the expression is false, the script jumps to the instructions that follow the else statement. If there is no else statement, the script jumps past the if statement entirely and continues from there.


The syntax for if is:

if (expression)


The result of the if expression is always either true or false. The following syntax is acceptable when there's only one instruction following the if and else statements:

if (ExampleVar == 10)
    Start();
else
    Stop();


Should more than one instruction follow the if or else statement, the { and } characters must be used to define an if statement block. With the { and } characters in place, JavaScript knows to execute all of the instructions within the block.

if (ExampleVar == 10) {
    Count = 1;
    Start();
} else {
    Count = 0; 
    Stop();
}
Expressions in if statements are not limited to the == equality operator. You can test if values are not equal to one another, greater than, less than, and more. For more information on the operators you can use, see the references list at the end of this column. It points to valuable JavaScript documentation, including documentation provided by Netscape.

Back to index

New

The new statement creates a new copy of an object. It is used in either of two ways:

  • To define a new Date object (Date is a built-in JavaScript object)
  • To define a new user-defined object


The syntax is the same with either use:

varname = new objectName(params);


  • varname is the name of the new object. Acceptable names are the same as for JavaScript variables. In fact, you can consider the created object as a JavaScript variable.

  • objectName is the name of the object. When using the built-in Date object, you use the word Date (note the capitalization -- this is mandatory). When using a user-defined object function, you provide the name of the object function.

  • params are one or more parameters that you pass to the object function, if needed.


The following example shows how to use the new statement to create a copy -- otherwise known as an "instance" -- of the Date object:

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources