Code block delimiter {} (Delimiter)

A delimiting token for a block of executable script source text.


ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
JavaScript syntax:-aLabel: { someScript }
Argument list:aLabelAn optional identifier to name the code block.
someScriptSome legal JavaScript source text

A block is a list of statements that form one syntactic unit enclosed in curly brace characters ( { } ).

This is particularly useful in conditional execution and iterative execution. Both of those are expected to operate on a single syntactic unit. A block allows that single syntactic unit to be composed of multiple lines of source script text.

Because the curly brace characters are used to delimit a block of code that comprises a list of semi-colon terminated statements, you do not need to place any semi-colons after the closing curly brace.

A block of code is most often used like this with a iterator or conditional test to either call the same section of code repetitively or to execute it as the result of a conditional expression returning a true value.

In compiled languages, variables declared inside a block are sometimes local to that block and are garbage-collected when the block exits. The ECMA standard indicates that variables created inside a code block will be global unless that code block is the body of a function. In ECMA-compliant interpreters, a block does not instantiate a new execution context, whereas in C language it does create a new scope within which the variables exist.

This means that variables created inside an 'if keyword' controlled compound statement will be function-local or globally accessible according to whether the 'if keyword' is in a function or global code section.

Braces must be used in pairs. Although the JavaScript interpreters may forgive you when you miss out some language elements, very subtle and difficult-to-diagnose errors can occur if you misplace a brace character.

Modern text editors give you a lot of help when balancing pairs of braces.

The associativity is left to right.

Refer to the Operator Precedence topic for details of execution order.

At version 1.2 of JavaScript, you can name the code block and use the labeled form of the break keyword to exit the block prematurely.

See also:Associativity, else if( ... ) ..., if( ... ) ..., if( ... ) ... else ..., Label, Operator Precedence, Punctuator


ECMA 262 edition 2 - section - 12.5

ECMA 262 edition 3 - section - 12.1

Wrox Instant JavaScript - page - 18