block - JavaScript 编辑
A block statement (or compound statement in other languages) is used to group zero or more statements. The block is delimited by a pair of braces ("curly brackets") and may optionally be labelled:
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.Syntax
Block Statement
{ StatementList }
Labelled Block Statement
LabelIdentifier: { StatementList }
StatementList
- Statements grouped within the block statement.
LabelIdentifier
- An optional label for visual identification or as a target for
break
.
Description
The block statement is often called 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. The opposite behavior is possible using an empty statement, where you provide no statement, although one is required.
Blocks are commonly used in association with if...else
and for
statements.
Examples
Block scoping rules with var or function declaration in non-strict mode
Variables declared with var
or created by function declarations in non-strict mode do not have block scope. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. For example:
var x = 1;
{
var x = 2;
}
console.log(x); // logs 2
This logs 2 because the var x
statement within the block is in the same scope as the var x
statement before the block.
In non-strict code, function declarations inside blocks behave strangely. Do not use them.
Block scoping rules with let, const or function declaration in strict mode
By contrast, identifiers declared with let
and const
do have block scope:
let x = 1;
{
let x = 2;
}
console.log(x); // logs 1
The x = 2
is limited in scope to the block in which it was defined.
The same is true of const
:
const c = 1;
{
const c = 2;
}
console.log(c); // logs 1 and does not throw SyntaxError...
Note that the block-scoped const c = 2
does not throw a SyntaxError: Identifier 'c' has already been declared
because it can be declared uniquely within the block.
In strict mode, starting with ES2015, functions inside blocks are scoped to that block. Prior to ES2015, block-level functions were forbidden in strict mode.
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Block statement' in that specification. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论