CodeZettaInterview Hub

JavaScript · Scope · Junior · Comparison

Compare `var`, `let`, and `const` in terms of scope, redeclaration, and reassignment.

Short Interview Answer

`var` is function-scoped and redeclarable; `let`/`const` are block-scoped; only `const` forbids reassignment of the binding.

Detailed Explanation

`var`: function/global scoped, hoisted as `undefined`, can be redeclared in the same scope, assignable anytime. `let`: block-scoped, TDZ until initialized, cannot redeclare in the same scope, can reassign. `const`: like `let` but the binding cannot be reassigned after initialization — objects/arrays held by `const` can still be mutated unless frozen. Prefer `const` by default, `let` when reassignment is needed, and avoid `var` in new code except for rare legacy interop.

Example

const user = { name: 'Ada' };
user.name = 'Grace'; // OK — mutation
// user = {}; // TypeError — rebinding

Interview Tip

Clarify that `const` ≠ immutable value; it means immutable binding.

Common Mistake

Saying `const` makes objects immutable.

Did you know this answer?