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

instance

class Calc {
	constructor() {
		this.count = 0 
	}

	increase() {
		this.count ++
	}
}

const calc = new Calc()
new Calc().increase()

console.log(calc.count)

We set the variable calc equal to a new instance of the Calc class. Then, we instantiate a new instance of Calc, and invoke the increase method on this instance. Since the count property is within the constructor of the Calc class, the count property is not shared on the prototype of Calc. This means that the value of count has not been updated for the instance calc points to, count is still 0.

Ref: https://bit.ly/2Hcpkm6