JavaScript let keyword

In this tutorial I will introduce you to new ECMAScript 6 let keyword. Variables declared with let keyword behave like other languages variables. These variables are unlike to the variables which are declared with var keyword. Let me show you example…

let

Variables which are declared with let statement are block scoped local variables which means they are limited in scope to the block, statement, or expression on which it is used. You can optionally initialize variables with value. This is unlike the var statement, which defines a variable globally, or locally to an entire function regardless of block scope.

Variables which are defined or declared with let keyword are not bind to window object.

Example with var and let

Running the above code produces the wrong results, it changes the outer x from 100 to 4 after the loop, as you can see from this output:

Now replace all the occurrences of var with let. Don’t change anything else yet.

Now we are seeing correct output. This is because unlike var variables which are function-scoped, let variables are block-scoped. they only exist in the block they are defined in.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a PHP Developer from last 6+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.