Labels

javascript (9) css (3) babel (1) nodejs (1) webpack (1)

Monday, November 14, 2016

Using let vs. var

There is already a lot out there on the usage of let versus the usage of var with Javascript so I am not explaining anything new. This is just a quick demonstration for those who need a quick answer.

let is available in ES6 (ES2015) only. You can check support for it on this site. What let allows you to do is keep your variables in the scope they are defined in. At the bottom of my demonstration you see let y = "yes"; inside an if statement while let y = "no"; before the statement. Inside the statement it holds the value "yes", but outside it is "no." var does not work this way as you can see in the same example. n is defined as "yes" outside the statement and then redefined as "no" inside the statement. Displaying n outside the statement shows we were using the same variable the entire time.

See the Pen let vs. var by Galastun (@galastun) on CodePen.

No comments:

Post a Comment