• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

1.6. 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:

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);

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.

Empty statements

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

Labeled statements

In 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


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

break ;
break label ;  // JavaScript 1.2


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

case constant-expression :
  [ break ; ]

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


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

continue ;
continue label ;  //JavaScript 1.2


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

  [ break ; ]


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 will be executed at least once:

while ( expression ) ;

This statement is new in JavaScript 1.2. In Navigator 4, the continue statement does not work correctly within do/while loops.


The export statement was introduced in Navigator 4. It makes the specified functions and properties accessible to other windows or execution contexts:

export expression [, expression... ];


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

for (initialize ; test ; increment)

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


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

for (variable in object)

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.


The function statement defines a function in a JavaScript program:

function funcname(args) {

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.


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

if ( expression )

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

if ( expression )

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

if ( expression )
else if ( expression2 )


The import statement was introduced in Navigator 4 along with export. It makes the named functions and variables available in the current window or execution context, or, in the second form of the statement, makes all properties and methods of the specified object available within the current context:

import expression [, expression];
import expression.* ;


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 ;


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


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 ]  . . .  ] ;


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

while ( expression )
    statement ;


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 use of with statements is discouraged.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint