Summary of JavaScript Statements

This chapter introduced each of the JavaScript language’s statements. Table 5-1 summarizes them, listing the syntax and purpose of each.

Table 5-1. JavaScript statement syntax

StatementSyntaxPurpose
breakbreak [label];

Exit from the innermost loop or switch or from named enclosing statement

casecase expression:

Label a statement within a switch

continuecontinue [label];

Begin next iteration of the innermost loop or the named loop

debuggerdebugger;

Debugger breakpoint

defaultdefault:

Label the default statement within a switch

do/whiledo statement while (expression);

An alternative to the while loop

empty;

Do nothing

forfor(init; test; incr) statement

An easy-to-use loop

for/infor (var in object) statement

Enumerate the properties of object

functionfunction name([param[,...]]) { body }

Declare a function named name

if/elseif (expr) statement1 [else statement2]

Execute statement1 or statement2

labellabel: statement

Give statement the name label

returnreturn [expression];

Return a value from a function

switchswitch (expression) { statements }

Multiway branch to case or default: labels

throwthrow expression;

Throw an exception

try

try { statements }

[catch { handler statements }]

[finally { cleanup statements }]

Handle exceptions

use strict"use strict";

Apply strict mode restrictions to script or function

varvar name [ = expr] [ ,... ];

Declare and initialize one or more variables

whilewhile (expression) statement

A basic loop construct

with with (

Get JavaScript: The Definitive Guide, 6th 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.