Block scope in JavaScript

·

1 min read

Block scope also known as compound statement used to combine multiple javascript code into a group and it is define by {}

We group multiple statements in a block so that we can use it where javascript expects a statement.

For example let an statement which require another statement just after it

if(true)

if we execute this only then it will through an error because it expect a single statement just after it so if we use a statement after it i.e.

if(true) console.log("hi there")

then it is fine, but if want to use multiple statement then we must have group them together and we can achieve that by grouping them together i.e.

if(true){
let x = 0;
console.log(x)
console.log("here {} grouped multiple statement into one")
}

So a group of statements is used when javascript expects a single statement for example if('true') expects a single statement and if we want to use multiple statements then we must have to use a Block which is wrapping up multiple statements.