JavaScript var, let, const differences explained

Day 5/50 – JavaScript Interview Question? Question: What is the difference between var, let, and const? Simple Answer: var is function-scoped and gets hoisted with undefined, let is block-scoped and has a Temporal Dead Zone, and const is also block-scoped but cannot be reassigned (though objects/arrays it references can still be mutated). 🧠 Why it matters in real projects: Using let and const prevents common bugs related to scope leakage and accidental global variables. Modern codebases typically avoid var entirely, using const by default and let only when reassignment is needed. 💡 One common mistake: Thinking const makes objects immutable. It only prevents reassignment of the variable itself. The object's properties can still be changed! 📌 Bonus: const user = { name: 'John' }; user.name = 'Jane'; // ✅ This works! user = {}; // ❌ TypeError: Assignment to constant // For true immutability, use Object.freeze() const frozen = Object.freeze({ name: 'John' }); frozen.name = 'Jane'; // Silently fails (or throws in strict mode) #JavaScript #WebDevelopment #Frontend #LearnInPublic #InterviewQuestions #Programming #TechInterviews

To view or add a comment, sign in

Explore content categories