Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A block statement (also known as a compound statement) is a lexical structure used to group zero or more statements into a single statement context. Delimited by a pair of curly braces {}, it establishes a distinct lexical environment for block-scoped variables and declarations.
{
  statement_1;
  statement_2;
  // ...
  statement_n;
}

Lexical Scoping Behavior

The primary mechanical function of a block statement is its interaction with variable declarations and the environment record:
  • Block-scoped declarations: Variables declared with let and const, as well as class declarations, are bound strictly to the block’s lexical environment. They are subject to the Temporal Dead Zone (TDZ) from the start of the block until the declaration is evaluated, and they cannot be referenced outside the block.
  • Function-scoped declarations: Variables declared with var are not scoped to the block. They bypass the block’s lexical environment and are hoisted to the nearest enclosing function or global environment record.
  • Function declarations: In strict mode ("use strict"), function declarations are block-scoped. In non-strict mode, their behavior is historically inconsistent across browsers but generally hoists to the enclosing function or global scope.
{
  var x = 10;
  let y = 20;
  const z = 30;
}

console.log(x); // 10 (var ignores block scope)

// The following lines are commented out because unhandled 
// ReferenceErrors would halt script execution immediately:
// console.log(y); // Throws ReferenceError: y is not defined
// console.log(z); // Throws ReferenceError: z is not defined

Evaluation and Syntax Rules

  • Not an Expression: A block statement is a statement, not an expression. It does not evaluate to a value that can be assigned to a variable or passed as an argument. Attempting to assign a block to a variable results in a SyntaxError (unless parsed as an object literal).
  • No Semicolon: Unlike most statements in JavaScript, a block statement does not require a terminating semicolon after the closing brace }.
  • Nesting: Block statements can be nested infinitely, with each nested block creating a new, child lexical environment.

Labeled Block Statements

A block statement can be prefixed with a label identifier. This allows the break statement to terminate the execution of the block and transfer control flow to the statement immediately following the block. continue cannot be used with a labeled block unless the block is part of an iteration statement.
targetLabel: {
  console.log("Execution starts");
  break targetLabel;
  console.log("This is never reached");
}
console.log("Execution resumes here");
Master JavaScript with Deep Grasping Methodology!Learn More