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);
x++
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
break

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

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:
  statements
  [ 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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.