What are the key differences between var, let, and const in JavaScript?
The short answer
var is function-scoped and gets hoisted with a default value of undefined. let and const are block-scoped and get hoisted too, but they throw a ReferenceError if you try to use them before their declaration (this is called the Temporal Dead Zone). The difference between let and const is that const cannot be reassigned after initialization, but let can.
Scoping — the most important difference
The biggest difference between var and let/const is how they handle scope.
var is function-scoped. That means a var variable is available anywhere inside the function where it was declared, regardless of blocks like if, for, or while.
function example() { if (true) { var name = 'Sarah'; } console.log(name); // 'Sarah' // name is accessible here because var is function-scoped // the if block does not create a new scope for var}let and const are block-scoped. A block is anything between curly braces {}. The variable only exists inside that block.
function example() { if (true) { let name = 'Sarah'; const age = 25; } console.log(name); // ReferenceError: name is not defined console.log(age); // ReferenceError: age is not defined // Both are trapped inside the if block}This matters a lot in loops:
for (var i = 0; i < 3; i++) { // do something}console.log(i); // 3 — var leaks out of the loopfor (let j = 0; j < 3; j++) { // do something}console.log(j); // ReferenceError — let stays inside the loopvar leaking out of blocks is a common source of bugs. This is one of the main reasons let and const were introduced in ES6.
Hoisting and the Temporal Dead Zone
All three — var, let, and const — are hoisted. But they behave differently after hoisting.
When JavaScript hoists a var declaration, it initializes the variable with undefined:
console.log(name); // undefined (not an error)var name = 'Sarah';JavaScript treats this as:
var name = undefined; // hoisted and initializedconsole.log(name); // undefinedname = 'Sarah'; // assignment stays in placeWhen JavaScript hoists let or const, it does not initialize them. They exist in memory but you cannot access them until the code reaches the declaration line. The space between the top of the block and the declaration is called the Temporal Dead Zone (TDZ).
console.log(name); // ReferenceError: Cannot access 'name'// before initializationlet name = 'Sarah';The variable name is hoisted (JavaScript knows it exists), but it is in the TDZ. Any attempt to read or write it before the let name line throws a ReferenceError.
Common Pitfalls
A common mistake candidates make is saying "let and const are not hoisted." They are hoisted — JavaScript knows about them at the top of the block. The difference is that they are not initialized with undefined like var. They sit in the Temporal Dead Zone until the declaration line is reached. Saying they are not hoisted is technically incorrect and interviewers will catch it.
const does not mean immutable
This is a very important distinction. const prevents reassignment, not mutation.
const age = 25;age = 30; // TypeError: Assignment to constant variableconst user = { name: 'Sarah', age: 25 };user.age = 30; // This works fineuser.email = 'a@b.com'; // This also works fineconsole.log(user); // { name: 'Sarah', age: 30, email: 'a@b.com' }You cannot point user to a different object, but you can change the properties inside the object. The same applies to arrays:
const colors = ['red', 'blue'];colors.push('green'); // Works finecolors[0] = 'yellow'; // Works finecolors = ['new array']; // TypeError: Assignment to constant variableThe reference (the pointer to the object or array in memory) is constant. The contents are not.
Common Pitfalls
A lot of candidates say "const makes things immutable" in interviews. It does not. If you want true immutability, you need Object.freeze() — and even that only freezes one level deep (it does not freeze nested objects). Getting this right shows the interviewer you understand how references work in JavaScript.
When to use each one
Here is a simple rule that most teams follow:
-
Use
constby default — for everything. If you do not need to reassign the variable, useconst. This makes your code easier to reason about because you know the variable will not change. -
Use
letwhen you need to reassign — loop counters, accumulators, values that change over time. -
Never use
var— there is no good reason to usevarin modern JavaScript.letdoes everythingvardoes but with better scoping.
// const for values that do not changeconst API_URL = 'https://api.example.com';const users = [];// let for values that changelet count = 0;let isLoading = true;for (let i = 0; i < 10; i++) { count = count + i;}Interview Tip
When asked "var vs let vs const," many candidates just list the differences. What separates a strong answer is explaining why these differences matter. Talk about how var leaking out of blocks causes real bugs, how the TDZ catches errors early instead of silently giving you undefined, and why const by default makes code more predictable. The interviewer wants to see that you understand the reasoning behind the language design, not just the syntax.
Why interviewers ask this
This question tests your understanding of scope, hoisting, and how JavaScript has evolved. It seems like a simple question, but it branches into deeper topics — closures (how let fixes the classic loop problem), hoisting (TDZ vs undefined), and immutability (references vs values). Interviewers use it as a gateway question. Your answer tells them how deep your JavaScript knowledge goes. If you can explain the nuances clearly, it opens the door for more advanced follow-ups. If you just list bullet points, the interviewer knows you memorized the answer without understanding it.