let is one of the fundamental improvements in modern JavaScript. Below is a compact, well-organized guide: what let does, why it matters, subtle gotchas (TDZ, hoisting, shadowing), comparisons with var/const, and practical examples you can copy/paste.
1. What let does — short answer
let declares a variable that is block scoped (i.e., visible only inside the nearest { ... }), can be reassigned, and is not added as a property of the global object (unlike var in the global scope).
2. Basic syntax & example
You may declare without initialization:
3. Block scope — concrete examples
Inside a block:
Inside a function:
4. Hoisting behaviour — clarified
- let declarations are hoisted (the JS engine knows the name), but they are not initialized until execution reaches the declaration line → TDZ.
- var is hoisted and initialized to undefined at function/global scope, so you can access it before its declaration (but it’s undefined).
