How JavaScript var, let, and const behave differently

Did you know? In JavaScript var, let and const are all hoisted, but they behave differently. Most people think let and const are not hoisted because their behavior does not match var. In reality, they’re placed in a special zone called the Temporal Dead Zone (TDZ). Check out these points for better clarity: 1. var - Hoisted + initialized as undefined 2. let & const - Hoisted but not initialized (that’s why you get ReferenceError) 3. var - Function scoped 4. let & const - Block scoped 5. const - Must be initialized at declaration 6. All global var - live in window object 7. let & const - stored in a separate “Script” scope console.log(a) // reference error let a = 20; isn’t because it’s not hoisted, it' s because a is inside the TDZ. Once you understand this, the behavior of JavaScript stops feeling random. #javascript #frontenddevelopment #webdevelopment #learninginpublic

To view or add a comment, sign in

Explore content categories