As mentioned in the previous article…. Before ES6, JavaScript had only two types of scope: Global Scope and Function Scope. With the let and const keywords we can talk about Block Scope. In this article let’s discuss more about the scope….

functional scope

var keyword has functional scope : Each function creates a new scope.

var has function scope

This is because the age variable declared inside the printAge function is scoped to the functional scope of this function.Thus variables declared inside the function are only available within the function.

Block Scope

var does not have block scope…so everything declared inside the block is also available in the outside of the block

what if we change it to let?

This is because let and const have block scope instead of function scope….so let’s see it by examples 👇

let has block scope
const has function scope

So these are the basic differences of var,let and const keywords. Hope you got an idea about var,let and const from these two articles !

You can find the part 1 from here👇https://medium.com/@shashiwadananirmani/javascript-var-vs-let-vs-const-part-1-5b48e0eeebeb

--

--