0 votes
1 view
in Web Technology by (47.8k points)

ECMAScript 6 introduced the let statement.

I've heard it that it's described as a "local" variable, but I'm still not quite sure how it behaves differently than the var keyword.

What are the differences? When should let be used over var?

1 Answer

0 votes
by (107k points)

The var keyword is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. If used outside any block then both are global.

Another point is that variables declared with let are not accessible before they are declared in their enclosing block. 

They are very similar when used like this outside a function block.

let me = 'go'; 

globally scoped var i = 'able'; 

The global variables which are defined with let will not be added as properties on the global window object like those defined with var.

console.log(window.me); 

console.log(window.i); 

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...