Backbencher.dev

JS Daily 7 - Block Scoped Let

Last updated on 27 Nov, 2020

What is the output of following code?

var a = 10;
{
  let a = 20;
}
console.log(a);
----o----

Output is 10.

First a variable a is declared in global scope using var and initialized with value 10. Then, a new block scoped variable a is declared using let and assigned with value 20. The block scoped variable works only within the block(ie inside the curly brackets{}). So, again outside the block, the printed a is the global a with value 10.

--- ○ ---
Joby Joseph
Web Architect