Jumps
Another category of JavaScript statements are jump
statements. As the name implies, these cause the JavaScript
interpreter to jump to a new location in the source code. The break
statement makes the interpreter jump
to the end of a loop or other statement. continue
makes the interpreter skip the rest
of the body of a loop and jump back to the top of a loop to begin a
new iteration. JavaScript allows statements to be named, or
labeled, and the break
and continue
can identify the target loop or
other statement label.
The return
statement makes
the interpreter jump from a function invocation back to the code that
invoked it and also supplies the value for the invocation. The
throw
statement raises, or
“throws,” an exception and is designed to work with the try/catch/finally
statement, which
establishes a block of exception handling code. This is a complicated
kind of jump statement: when an exception is thrown, the interpreter
jumps to the nearest enclosing exception handler, which may be in the
same function or up the call stack in an invoking function.
Details of each of these jump statements are in the sections that follow.
Labeled Statements
Any statement may be labeled by preceding it with an identifier and a colon:
identifier
:
statement
By labeling a statement, you give it a name that you can use to refer to it elsewhere in your program. You can label any statement, although it is only useful to label statements that have bodies, such as loops and conditionals. By giving a loop ...
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.