Team LiB   Previous Section   Next Section

1.5 Statements

A JavaScript program is a sequence of JavaScript statements. Most JavaScript statements have the same syntax as the corresponding C, C++, and Java statements.

1.5.1 Expression statements

Every JavaScript expression can stand alone as a statement. Assignments, method calls, increments, and decrements are expression statements. For example:

s = "hello world";
x = Math.sqrt(4);
x++;

1.5.2 Compound statements

When a sequence of JavaScript statements is enclosed within curly braces, it counts as a single compound statement. For example, the body of a while loop consists of a single statement. If you want the loop to execute more than one statement, use a compound statement. This is a common technique with if, for, and other statements described later.

1.5.3 Empty statements

The empty statement is simply a semicolon by itself. It does nothing, and is occasionally useful for coding empty loop bodies.

1.5.4 Labeled statements

As of JavaScript 1.2, any statement can be labeled with a name. Labeled loops can then be used with the labeled versions of the break and continue statements:

label : statement

1.5.5 Alphabetical statement reference

The following paragraphs document all JavaScript statements, in alphabetical order.

break

The break statement terminates execution of the innermost enclosing loop, or, in JavaScript 1.2 and later, the named loop:

break ;
break label ;

case

case is not a true statement. Instead it is a keyword used to label statements within a JavaScript 1.2 or later switch statement:

case constant-expression :
    statements
    [ break ; ]

Because of the nature of the switch statement, a group of statements labeled by case should usually end with a break statement.

continue

The continue statement restarts the innermost enclosing loop, or, in JavaScript 1.2 and later, restarts the named loop:

continue ;
continue label ;
default

Like case, default is not a true statement, but instead a label that may appear within a JavaScript 1.2 or later switch statement:

default:
    statements
    [ break ; ]
do/while

The do/while loop repeatedly executes a statement while an expression is true. It is like the while loop, except that the loop condition appears (and is tested) at the bottom of the loop. This means that the body of the loop is executed at least once:

do
    statement
while ( expression ) ;

This statement was introduced in JavaScript 1.2. In Netscape 4, the continue statement does not work correctly within do/while loops.

for

The for statement is an easy-to-use loop that combines the initialization and increment expressions with the loop condition expression:

for (initialize ; test ; update )
    statement

The for loop repeatedly executes statement as long as the test expression is true. It evaluates the initialize expression once before starting the loop and evaluates the update expression at the end of each iteration.

for/in

The for/in statement loops through the properties of a specified object:

for (variable in object)
    statement

The for/in loop executes a statement once for each property of an object. Each time through the loop, it assigns the name of the current property to the specified variable. Some properties of pre-defined JavaScript objects are not enumerated by the for/in loop. User-defined properties are always enumerated.

function

The function statement defines a function in a JavaScript program:

function funcname ( args ) {
    statements
}

This statement defines a function named funcname, with a body that consists of the specified statement, and arguments as specified by args. args is a comma-separated list of zero or more argument names. These arguments can be used in the body of the function to refer to the parameter values passed to the function.

if/else

The if statement executes a statement if an expression is true:

if ( expression )
    statement

When an else clause is added, the statement executes a different statement if the expression is false:

if ( expression )
    statement
else
    statement2

Any else clause may be combined with a nested if/else statement to produce an else if statement:

if ( expression )
    statement
else if ( expression2 )
    statement2
else
    statement3
return

The return statement causes the currently executing function to stop executing and return to its caller. If followed by an expression, the value of that expression is used as the function return value:

return ;
return expression ;
switch

The switch statement is a multi-way branch. It evaluates an expression and then jumps to a statement that is labeled with a case clause that matches the value of the expression. If no matching case label is found, the switch statement jumps to the statement, if any, labeled with default:

switch ( expression ) {
    case constant-expression: statements
    [ case constant-expression: statements ]
    [  . . .  ]
    default: statements
}

Each set of statements within a switch statement is usually terminated with a break or return so that execution does not fall through from one case to the next one.

throw

The throw statement signals an error, or throws an exception. It causes program control to jump immediately to the nearest enclosing exception handler (see the try/catch/finally statement). The throw statement is defined by ECMAv3 and implemented in JavaScript 1.5. Its syntax is:

throw expression ;

The expression may evaluate to any type. (See Error in the reference section.)

try/catch/finally

The try/catch/finally statement is JavaScript's exception handling mechanism. It is defined by ECMAv3 and implemented in JavaScript 1.5. Its syntax is:

try {
    statements
}
catch ( argument ) {
    statements
}
finally {
    statements
}

The try clause of this statement defines a block of code for which exceptions and errors are to be handled. If a program error occurs, or an exception is thrown within the try block, control jumps to the exception-handling statements in the catch clause. This clause includes a single argument or local variable; the value that was thrown by the exception is assigned to this local variable so that it can be referred to by the statements of the catch clause. The finally clause contains statements that are executed after the try or catch clauses, whether or not an exception is thrown. The catch and finally clauses are optional, but you cannot omit both of them.

var

The var statement declares and optionally initializes one or more variables. Variable declaration is optional in top-level code, but is required to declare local variables within function bodies:

var name [ = value ] [ , name2 [ = value2 ]  . . .  ] ;
while

The while statement is a basic loop. It repeatedly executes a statement while an expression is true:

while ( expression )
    statement ;
with

The with statement adds an object to the scope chain, so that a statement is interpreted in the context of the object:

with ( object )
    statement ;

The with statement has some complex and non-intuitive side effects; its use is strongly discouraged.

    Team LiB   Previous Section   Next Section