flâneur — a map of the web's best reading

javascript - What is the difference between "let" and "var"? - Stack Overflow

stackoverflow.com · 12,466 words · saved by 1 readers

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