Hướng dẫn block statement javascript
A block statement is used to group zero or more statements. The block is delimited by a pair of braces ("curly brackets") and contains a list of zero or more statements and declarations. Statements and declarations grouped within the block statement. The block statement is often called the compound statement in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Combining statements into blocks is a common practice in JavaScript, especially when used in association with control flow statements like
In addition,
combined with block-scoped declarations like Variables declared with
This logs 2 because the In non-strict code, function declarations inside blocks behave strangely. Do not use them. Block scoping rules with let, const, class, or function declaration in strict modeBy contrast,
identifiers declared with
The The same is true of
Note that the block-scoped In strict mode, function declarations inside blocks are scoped to that block and are hoisted.
ExamplesUsing a block statement as the body of a for loopA
If you want to use more than one statement in the loop body, you can group them into one block statement:
Using a block statement to encapsulate data
Specifications
Browser compatibilityBCD tables only load in the browser See also |