| Previous Contents Index Next |
| Core JavaScript Reference 1.5 |
Chapter 3 Chapter 3 Statements
This chapter describes all JavaScript statements. JavaScript statements consist of keywords used with the appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semicolon.Syntax conventions: All keywords in syntax statements are in bold. Words in italics represent user-defined names or statements. Any portions enclosed in square brackets, [ ], are optional. {statements} indicates a block of statements, which can consist of zero or more statements delimited by a curly braces { }.
The following table lists statements available in JavaScript.
Table 3.1 JavaScript statements.
break
Terminates the current while or for loop and transfers program control to the statement following the terminated loop.
const
Declares a global constant, optionally initializing it to a value.
continue
Terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration.
do...while
Executes the specified statements until the test condition evaluates to false. Statements execute at least once.
export
Allows a signed script to provide properties, functions, and objects to other signed or unsigned scripts.
for
Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop.
for...in
Iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements.
function
Declares a function with the specified parameters. Acceptable parameters include strings, numbers, and objects.
if...else
Executes a set of statements if a specified condition is true. If the condition is false, another set of statements can be executed.
import
Allows a script to import properties, functions, and objects from a signed script that has exported the information.
label
Provides an identifier that can be used with break or continue to indicate where the program should continue execution.
return
switch
Allows a program to evaluate an expression and attempt to match the expression's value to a case label.
throw
try...catch
Marks a block of statements to try, and specifies a response should an exception be thrown.
var
while
Creates a loop that evaluates an expression, and if it is true, executes a block of statements. The loop then repeats, as long as the specified condition is true.
with
Use the break statement to terminate a loop, switch, or label statement.Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated loop.
ECMA-262 (for the unlabeled version)
ECMA-262, Edition 3 (for the labeled version)
label
Description
The break statement includes an optional label that allows the program to break out of a labeled statement. The statements in a labeled statement can be of any type.
Examples
The following function has a break statement that terminates the while loop when e is 3, and then returns the value 3 * x.function testBreak(x) {
var i = 0;
while (i < 6) {
if (i == 3)
break;
i++;
}
return i*x;
}
See also
continue, label, switch
Declares a readonly, named constant.
JavaScript 1.5, NES 6.0 (Netscape extension, C engine only),
Syntax
const constname [= value] [..., constname [= value] ]
varname
value
Description
Creates a constant that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables.The value of a constant cannot change through re-assignment, and a constant cannot be re-declared.
A constant cannot share the same name as a function or variable in the same scope.
document.writeln("a is " + a + ".");
Restarts a while, do-while, for, or label statement.
ECMA-262 (for the unlabeled version)
ECMA-262, Edition 3 (for the labeled version)
label
The continue statement can now include an optional label that allows the program to terminate execution of a labeled statement and continue to the specified labeled statement. This type of continue must be in a looping statement identified by the label used by continue.
Description
In contrast to the break statement, continue does not terminate the execution of the loop entirely: instead,
Examples
Example 1. The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12.i = 0;
n = 0;
while (i < 5) {
i++;
if (i == 3)
continue;
n += i;
}Example 2. In the following example, a statement labeled checkiandj contains a statement labeled checkj. If continue is encountered, the program continues at the top of the checkj statement. Each time continue is encountered, checkj reiterates until its condition returns false. When false is returned, the remainder of the checkiandj statement is completed. checkiandj reiterates until its condition returns false. When false is returned, the program continues at the statement following checkiandj.
If continue had a label of checkiandj, the program would continue at the top of the checkiandj statement.
checkiandj :
while (i<4) {
document.write(i + "<BR>");
i+=1;checkj :
while (j>4) {
document.write(j + "<BR>");
j-=1;
if ((j%2)==0)
continue checkj;
document.write(j + " is odd.<BR>");
}
document.write("i = " + i + "<br>");
document.write("j = " + j + "<br>");
}
Executes the specified statements until the test condition evaluates to false. Statements execute at least once.
Syntax
do
statements
while (condition);
Examples
In the following example, the do loop iterates at least once and reiterates until i is no longer less than 5.do {
i+=1;
document.write(i);
while (i<5);
Allows a signed script to provide properties, functions, and objects to other signed or unsigned scripts.This feature is not in ECMA 262, Edition 3.
Syntax
export name1, name2, ..., nameN
export *
nameN
*
Exports all properties, functions, and objects from the script.
Description
Typically, information in a signed script is available only to scripts signed by the same principals. By exporting properties, functions, or objects, a signed script makes this information available to any script (signed or unsigned). The receiving script uses the companion import statement to access the information.
See also
import
Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop.
Syntax
for ([initial-expression]; [condition]; [increment-expression]) {
statements
}
Examples
The following for statement starts by declaring the variable i and initializing it to 0. It checks that i is less than nine, performs the two succeeding statements, and increments i by 1 after each pass through the loop.for (var i = 0; i < 9; i++) {
n += i;
myfunc(n);
}
Iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements.
Syntax
for (variable in object) {
statements
}
variable
Variable to iterate over every property, optionally declared with the var keyword. This variable is local to the function, not to the loop.
object
statements
Examples
The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.function show_props(obj, objName) {
var result = "";
for (var i in obj) {
result += objName + "." + i + " = " + obj[i] + "\n";
}
return result;
}
Declares a function with the specified parameters. Acceptable parameters include strings, numbers, and objects.
JavaScript 1.5, NES 6.0: added conditional function declarations (Netscape extension).
Syntax
function name([param] [, param] [..., param]) {
statements
}You can also define functions using the Function constructor and the function operator; see Function and function.
name
param
The name of an argument to be passed to the function. A function can have up to 255 arguments.
statements
Description
To return a value, the function must have a return statement that specifies the value to return.A function created with the function statement is a Function object and has all the properties, methods, and behavior of Function objects. See Function for detailed information on functions.
Netscape supports conditional function declarations, whereby a function can be declared based on the evaluation of a condition. If the condition evaluates to true, the function is declared. Otherwise it is not declared.
A function can also be declared inside an expression. In this case the function is usually anonymous. See page 254.
Examples
The following code declares a function that returns the total dollar amount of sales, when given the number of units sold of products a, b, and c.function calc_sales(units_a, units_b, units_c) {
return units_a*79 + units_b*129 + units_c*699
}In the following script, the one function is always declared. The zero function is declared because 'if(1)' evaluates to true:
<SCRIPT language="JavaScript1.5">
<!--
function one()
document.writeln("This is one.");
if (1)
function zero()
{
document.writeln("This is zero.");
}
}
</SCRIPT>However, if the script is changed so that the condition becomes 'if (0)', function zero is not declared and cannot be invoked on the page.
Executes a set of statements if a specified condition is true. If the condition is false, another set of statements can be executed.
Syntax
if (condition) {
statements1
}
[else {
statements2
}]
Description
You should not use simple assignments in a conditional statement. For example, do not use the following code:if(x = y)
{
/* do the right thing */
}If you need to use an assignment in a conditional statement, put additional parentheses around the assignment. For example, use if( (x = y) ).
Examples
if (cipher_char == from_char) {
result = result + to_char
x++}
else
result = result + clear_char
Allows a script to import properties, functions, and objects from a signed script that has exported the information.This feature is not in ECMA 262, Edition 3.
Syntax
import objectName.name1, objectName.name2, ..., objectName.nameN
import objectName.*
objectName
name1,
name2,
nameN
List of properties, functions, and objects to import from the export file.
*
Imports all properties, functions, and objects from the export script.
Description
The objectName parameter is the name of the object that will receive the imported names. For example, if f and p have been exported, and if obj is an object from the importing script, the following code makes f and p accessible in the importing script as properties of obj.Typically, information in a signed script is available only to scripts signed by the same principals. By exporting (using the export statement) properties, functions, or objects, a signed script makes this information available to any script (signed or unsigned). The receiving script uses the import statement to access the information.
The script must load the export script into a window, frame, or layer before it can import and use any exported properties, functions, and objects.
See also
export
Provides a statement with an identifier that lets you refer to it using a break or continue statement.
For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.
label
statement
Statements. break can be used with any labeled statement, and continue can be used with looping labeled statements.
Examples
For an example of a label statement using break, see break. For an example of a label statement using continue, see continue.
Specifies the value to be returned by a function.
expression
Examples
The following function returns the square of its argument, x, where x is a number.function square(x) {
return x * x;
}
Allows a program to evaluate an expression and attempt to match the expression's value to a case label.
Syntax
switch (expression){
case label :
statements;
break;
case label :
statements;
break;
...
default : statements;
}
expression
label
statements
Block of statements that is executed once if expression matches label.
Description
If a match is found, the program executes the associated statement. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.The program first looks for a label matching the value of expression and then executes the associated statement. If no matching label is found, the program looks for the optional default statement, and if found, executes the associated statement. If no default statement is found, the program continues execution at the statement following the end of switch.
The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.
Examples
In the following example, if expression evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When break is encountered, the program breaks out of switch and executes the statement following switch. If break were omitted, the statement for case "Cherries" would also be executed.switch (i) {
case "Oranges" :
document.write("Oranges are $0.59 a pound.<BR>");
break;
case "Apples" :
document.write("Apples are $0.32 a pound.<BR>");
break;
case "Bananas" :
document.write("Bananas are $0.48 a pound.<BR>");
break;
case "Cherries" :
document.write("Cherries are $3.00 a pound.<BR>");
break;
default :
document.write("Sorry, we are out of " + i + ".<BR>");
}
document.write("Is there anything else you'd like?<BR>");
Throws a user-defined exception.
expression
Description
Use the throw statement to throw an exception. When you throw an exception, an expression specifies the value of the exception. The following code throws several exceptions.throw "Error2"; // generates an exception with a string value
throw 42; // generates an exception with the value 42
throw true; // generates an exception with the value true