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
whileloop 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
breakandcontinuestatements:label: statement
-
break The
breakstatement terminates execution of the innermost enclosing loop or, in JavaScript 1.2, the named loop:break ; break label ; // JavaScript 1.2-
case caseis not a true statement. Instead it is a keyword used to label statements within a JavaScript 1.2switchstatement:case constant-expression: statements [ break ; ]
Because of the nature of the
switchstatement, a group of statements labeled by case ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access