🟢 EasyCore JS📖 Theory Question

What is the difference between var, let, and const?

💡

Hint

Think about scope, hoisting, and reassignment

Full Answer

var is function-scoped and hoisted (initialized as undefined). let and const are block-scoped and in a Temporal Dead Zone before declaration.

var x = 1;
let y = 2;    // block-scoped, reassignable
const z = 3;  // block-scoped, binding locked

if (true) {
  var a = 'leaks out';
  let b = 'stays in';
}
console.log(a); // 'leaks out'
console.log(b); // ReferenceError

const doesn't make values immutable — objects/arrays can still be mutated. The binding is locked, not the value.

💡 Default to const, use let only when reassignment is needed. Avoid var.

More Core JS Questions

🟢 EasyExplain closures with a practical example.→🟢 EasyWhat is hoisting in JavaScript?→🟢 EasyExplain the event loop, call stack, and microtask queue.→🟢 EasyWhat is the difference between == and ===?→

Practice this in a timed sprint →

5 free questions, no signup required

âš¡ Start Sprint