Our volunteers haven't translated this article into हिन्दी (भारत) yet. Join us and help get the job done!
You can also read the article in English (US).

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 curly brackets and may optionally be labelled:

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.

Block Scoping Rules

With var

Variables declared with var do not have block scope. Variables introduced with 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. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java. 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 C or Java, the equivalent code would have outputted 1.

With let and const

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.

With function

A function declaration is also limited in scope inside the block where the declaration occurs:

foo('outside');  // TypeError: foo is not a function
{
  function foo(location) {
   console.log('foo is called ' + location);
  }
  foo('inside'); // works correctly and logs 'foo is called inside'
}

To be more precise, the block statement is preventing the function declaration from being hoisted to the top of the scope. The function is behaving as if it were defined as a function expression and, as such, it is only the implicit variable declaration that gets hoisted to the top of the scope:

foo;  // returns undefined
{
  function foo(location) {
   console.log('foo is called ' + location);
  }
  foo('inside'); // works correctly and logs 'foo is called inside'
}

Consequently, this means that if we move the call to foo below the block statement that there will be no error:

{
  function foo(location) {
   console.log('foo is called ' + location);
  }
  foo('inside'); // works correctly and logs 'foo is called inside'
}
foo('outside');  // works correctly and logs 'foo is called outside'

Specifications

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'Block statement' in that specification.
Draft  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Block statement' in that specification.
Standard  
ECMAScript 5.1 (ECMA-262)
The definition of 'Block statement' in that specification.
Standard  
ECMAScript 3rd Edition (ECMA-262)
The definition of 'Block statement' in that specification.
Standard  
ECMAScript 1st Edition (ECMA-262)
The definition of 'Block statement' in that specification.
Standard Initial definition. Implemented in JavaScript 1.0.

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support

See also

दस्तावेज़ टैग और योगदानकर्ता

अंतिम अद्यतन: jakubboucek,