Interview Question

Difference between var, let, and const?

var is function-scoped; let and const are block-scoped; const blocks reassignment.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

var, let, and const create bindings with different scope and initialization rules. • `var` is function-scoped and its declaration is instantiated with undefined. • `let` and `const` are block-scoped and remain uninitialized in the temporal dead zone. • `const` requires an initializer and prevents reassignment of the binding, not mutation of an object.

Example

Code
const user = { name: 'Maya' };
user.name = 'Asha';
let count = 1;
count += 1;

Quick Revision

var is function-scoped; let and const are block-scoped; const blocks reassignment.