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

Get Webmaster in a Nutshell, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.