w3resource

JavaScript: Block statement

Description

Block statements are commonly used with control flow statements i.e. while, for, if etc. The block is delimited by a pair of curly brackets.

Syntax

{ 
statement_1 
statement_2
 .    .    . 
statement_n 
} 

Parameters

statement_1, statement_2, .... , statement_n

Where statements are grouped within the block statement.

Example:

 if x>10
  {
  y=12
  z=20
  }
  or while ( roll_no <= 40 )
  {
  roll_no++
  }

JavaScript does not support block scope. In the following example output of the variable a will be 1 because var a statement within or before condition have same scope whereas in Java or C language the output of the same code will be 101.

var a = 101
  {
  a = 1;
  }
  console.log(a) 
  //output 1

Previous: JavaScript: void operator
Next: JavaScript: if...else statements

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

Returns the unique symmetric difference between two arrays, not containing duplicate values from either array

Example:

const uniqueSymmetricDifference = (a, b) => [
  ...new Set([...a.filter(v => !b.includes(v)), ...b.filter(v => !a.includes(v))])
];
console.log(uniqueSymmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
console.log(uniqueSymmetricDifference([1, 2, 2], [1, 3, 1])); // [2, 3]

Output:

[3, 4]
[2, 3]