javascript - What is the difference between "let" and "var"? - Stack Overflow
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? The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope). The reason why let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript. Take a look at this example from another Stack Overflow question: My value: 3 was output to console each time funcs[j](); was invoked since anonymous functions were bound to the same variable. People had to create immediately invoked functions to capture correct values from the loops but that was also hairy. Variables declared with var keyword are hoisted and initialized which means they are accessible in their
javascript - What is the difference between "let" and "var"? - Stack Overflow The 2026 Annual Developer Survey is live— take the Survey today! Collectives™ on Stack Overflow Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives Stack Internal Knowledge at work Bring the best of human thought and AI automation together at your work. Explore Stack Internal What is the difference between "let" and "var"? Ask Question Asked 17 years, 3 months ago Modified 2 months ago Viewed 2.4m times 6414 ECMAScript 6 introduced the let declar
Explore this link on the map →related reading
- Variablesjavascript.info
- CSC 151 - Naming values with local bindingseikmeier.sites.grinnell.edu
- Scope (computer programming) - Wikipediaen.wikipedia.org
- A fucking rant about fucking const vs fucking let - @jamiebuildsjamie.build
- Variables and Mutability - The Rust Programming Languagedoc.rust-lang.org
- What the fuck is a closure? ・ Dan’s JavaScript Glossarywhatthefuck.is
- A half-hour to learn Rustfasterthanli.me
- Resolving and Binding · Crafting Interpreterscraftinginterpreters.com
- Python Scope and the LEGB Rule: Resolving Names in Your Code – Real Pythonrealpython.com
- Functions — reusable blocks of code - Learn web development | MDNdeveloper.mozilla.org
- Statements and State · Crafting Interpreterscraftinginterpreters.com
- Log In to Just JavaScript | Just JavaScriptjustjavascript.com