Block Statements

JavaScript has "statement blocks" that combine multiple lines into a compound statement. Block statements are enclosed in curly brackets { } and do not require an ending semicolon. We'll use block statements when writing conditional statements in Unit 4.

block

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:

var x = 1;
let y = 1;

if (true) {
  var x = 2;
  let y = 2;
}

console.log(x);
// expected output: 2

console.log(y);
// expected output: 1

Source: Mozilla, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block
Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.