Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What's the difference between using “let” and “var” ?

"Let " is the statement introduced by ES6(ECMA Script 6).  What is this "Let" and how it behaves . Let us discuss about this in this post .

What is "let"

We are very familiar with key word "var" , which will help us to create a variable in Java script .
In ES6 this "let " word used to declare a variable . but both "let " and "var" behaves differently  .

The main difference is scoping. var will be scoped to the nearest function block and let will be scoped to the nearest enclosing block (both "let " and "var" are global if outside any block), which can be smaller than a function block.

And one more thing to be noted is , variables declared with let are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.

This post first appeared on WEB Developement, please read the originial post: here

Share the post

What's the difference between using “let” and “var” ?


Subscribe to Web Developement

Get updates delivered right to your inbox!

Thank you for your subscription