Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

What is the difference between "let" and "var"?

ECMAScript 6 introduced the let statement.

I've heard 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 instead of var?

Answer*

Cancel
6
  • 23
    regarding answer v4: i IS known everywhere in the function-block! It starts as undefined (due to hoisting) until you assign a value! ps: let is also hoisted (to the top of it's containing block), but will give a ReferenceError when referenced in the block before first assignment. (ps2: I'm a pro-semicolon kinda guy but you really don't need a semicolon after a block ). That being said, thanks for adding the reality-check regarding support!
    – GitaarLAB
    Commented May 21, 2016 at 4:41
  • @GitaarLAB : According to the Mozilla Developer Network : "In ECMAScript 2015, let bindings are not subject to Variable Hoisting, which means that let declarations do not move to the top of the current execution context." - Anyway, I made a few improvements to my answer that should clarify the difference in hoisting behavior between let and var! Commented Feb 26, 2018 at 23:37
  • 2
    Your answer improved a lot (I thoroughly checked). Note that same link you referenced in your comment also says: "The (let) variable is in a "temporal dead zone" from the start of the block until the initialization is processed." That means that the 'identifier' (the text-string 'reserved' to point to 'something') is already reserved in the relevant scope, otherwise it would become part of the root/host/window scope. To me personally, 'hoisting' means nothing more than reserving/linking declared 'identifiers' to their relevant scope; excluding their initialization/assignment/modifyability!
    – GitaarLAB
    Commented Mar 1, 2018 at 18:16
  • And..+1. That Kyle Simpson article you linked is an excellent read, thank you for that! It is also clear about the "temporal dead zone" aka "TDZ". One interesting thing I'd like to add: I've read on MDN that let and const were recommended to only use when you actually need their additional functionality, because enforcing/checking these extra features (like write-only const) result in 'more work' (and additional scope-nodes in the scope-tree) for the (current)engine(s) to enforce/check/verify/setup.
    – GitaarLAB
    Commented Mar 1, 2018 at 18:17
  • 2
    Note that MDN says that IE DOES interpret let correctly. Which is it? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 6, 2019 at 12:42