var vs let vs const — JS Technical Interview Questions & Answers

Side-by-side comparison table.

var vs let vs const

  • var vs let vs const (diff) is a JS topic. In plain words you use it for let total = 0 in a cart script. Don’t start with a slogan — start with that picture.
  • Smallest example: let for change, const for fixed. Type it, run it, and say what you see. If you can do that from memory, you know var vs let vs const (diff).
  • From the example next to this theory: Side-by-side comparison table.
  • Trap: var leak / const reassign. Fix that before you talk about advanced JS.
  • Viva: what is var vs let vs const (diff)? Then show let for change, const for fixed. Then name the trap.

Technical Interview Tip: What is var vs let vs const (diff)? Show this: let for change, const for fixed. Trap: var leak / const reassign.

Code Examples for var vs let vs const

var vs let vs const — working example

function demo() {
  if (true) {
    var a = 1;
    let b = 2;
    const c = 3;
    console.log(b, c);
  }
  console.log(a); // 1 — var is function-scoped
  // console.log(b); // ReferenceError — let is block-scoped
}
demo();
console.log(typeof undeclaredVar); // undefined (never declared with let/const)

var vs let vs const — var is function-scoped and hoisted; let/const are block-scoped. Prefer const, then let.

How to Explain var vs let vs const to an Interviewer

What is var vs let vs const (diff)?

Used for let total = 0 a cart script Not just the heading

Give one small example of var vs let vs const (diff).

let for change, const for fixed Side-by-side comparison table

What mistake do beginners make with var vs let vs const (diff)?

var leak / const reassign Show the fix

Where do you use var vs let vs const (diff)?

a cart script let total = 0