Web Programming Desktop Reference 6in1
Chapter 22
JavaScript Statements Java
The statements used to control program flow in JavaScript are
similar to those used in Java and C. A statement can span several
lines if needed or several statements can be placed on the same
line.
There are a couple of important items to remember. First, blocks of statements, such as a function definition, must be enclosed in curly braces. This is how JavaScript delineates blocks of code. Second, a semicolon must be placed between all statements. Without a semicolon, script behavior is unpredictable.
Since JavaScript is not strict in its formatting, you must provide
the line breaks and indentation to make sure the code is readable
and easy to understand later.
break
break terminates the current for or while loop and passes control to the first statement after the loop.
Usage
The following example adds elements on a form, assuming that all the elements contain numeric values. If a 0 is encountered, the adding stops:
function checkValues(form) {
var total
for (I=0; I<=form.elements.length; I++) {
if (element[I].value = "0") {
break; }
else {
total += I;
document.write("The running total is
}
return total
}
comment
These are notes from the script author that are ignored by the interpreter. Single line comments are preceded by //. Multiple line comments begin with /* and end with */.
Usage
/* These comments could start here
and
end
down here. */
...statements...
// This comment is limited to this line only.
continue
continue passes control to the condition in a while loop and to the update expression in a for loop. The important difference from break is that the loop is not terminated.
Usage
The following example adds elements on a form, assuming that all the elements contain numeric values. If a value less than 0 is encountered, it is not included in the running total:
function checkValues(form) {
var total
for (I=0; I<=form.elements.length; I++) {
if (element[I].value < 0) {
continue; }
else {
total += I;
document.write("The running total is "+total); }
}
return total
}
for
for creates a loop with three optional expressions enclosed in parentheses and separated by semicolons, followed by a set of statements to be executed during the loop:
for (initialExpression; condition; updateExpression) {
statements...
}
The initial expression is used to initialize the counter variable, which can be a new variable declared with var.
The condition expression is evaluated on each pass through the loop. If the condition is true, the loop statements are executed. If the condition is omitted, then it defaults to true, and the loop continues until an error or break is reached.
The update expression is used to increment the counter variable. It is also optional and can be updated programatically within the loop statements.
Usage
for creates a loop that continues until an error occurs or a break statement is executed. The increment variable is increased by two each time through the loop:
for (var increment=0; ; increment+=2) {
...statements...
}
The following example is a loop that does not update its counter. If the counter is never updated in the course of the statements, then the value will remain ten:
for (var increment=10; increment<101; ) {
...statements...
}
for in
This iterates a variable for all of properties of an object. For
each property, for...in
executes the statement block:
for (objectVariable) {
...statements...
}
Usage
for...in is a useful function for debugging because of its ability to display all of the properties of an object in one loop:
function objectDisplay (obj) {
var displayLine;
for (var prop in obj) {
displayLine = obj.name + "." + prop + " = " + obj[prop];
document.write(displayLine + "<BR>")
}
document.write("End of object " + obj.name)
}
function
This declares a JavaScript function with a name and parameters. To return a value, the function must include a return statement. A function definition cannot be nested within another function:
function name ([parameter [...,parameter]]) {
statements...
}
if else
A conditional statement that executes the first set of statements if the condition is true and the statements following else if false. if...else statements can be nested to any level. If single statements are used after the statements, curly braces are not needed:
if (condition) {
...statements...
} [else {
...statements...
}]
Usage
The following example converts minutes to a two-digit number for use in a clock display:
function makeMinutes() {
var minString = "";
var now = new Date();
var min = Date.getMinutes();
if (min < 10) {
minString += ":0" + min; }
else {
minString += ":" + min; }
return minString
}
return
return specifies a value to be returned by a function:
return expression;
Usage
The following example takes three strings and puts them together, separated by commas:
function stringAssemble (string1, string2, string3) {
return string1 + ", " + string2 + ", " + string3
}
var
var declares a variable and optionally initializes it to a value. The scope of a variable is the current function or-when declared outside a function-the current document:
var variableName [=value] [..., variableName [=value]]
Usage
The globalString variable can be used in any function or script in the current document, where