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…


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.

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

Leave a Reply

Your email address will not be published. Required fields are marked *