LearnThatStack Ace your next interview
JavaScript · question
Question 1 of 126

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

beginner
← All JavaScript questions
Re-explain

var is function-scoped, while let and const are block-scoped. const also stops the variable from being reassigned.

var can be redeclared and updated. It is hoisted and starts as undefined, so reading it before its line gives undefined instead of an error.

let can be updated but not redeclared in the same scope. It is hoisted but not initialized, so reading it before its declaration throws a ReferenceError.

const can be neither updated nor redeclared, and it must be initialized where it is declared. It fixes the binding, not the value, so an object held in a const can still change.

var a = 1;     // Function scoped
let b = 2;     // Block scoped
const c = 3;   // Block scoped, immutable binding
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Why does a setTimeout inside a for loop with var log the same number every time, and how does let change that? var gives one shared variable for the whole loop, so every callback sees the final value. let makes a fresh binding for each iteration.
  • You said let throws if you read it early. What is that window called, and where does it start and end? The temporal dead zone runs from the start of the block to the declaration line. typeof inside it throws too, unlike for a name that was never declared.
  • If const doesn't make an object immutable, how would you stop its properties from changing? Object.freeze blocks property changes, but only one level deep. Nested objects need their own freeze.
  • In a plain browser script, what's different about a top-level var versus a top-level let? A top-level var becomes a property of the global object, like window.x. let and const are global too, but they don't attach to it.
  • Which of the three do you use by default in new code, and why? A strong answer defaults to const, uses let only when a value must be reassigned, drops var, and names the bug each choice prevents.

What you can say

  1. The biggest difference is scope. var is scoped to the whole function, while let and const are scoped to the block they're declared in, like an if or a loop body.
  2. var can be redeclared and reassigned. It's hoisted and starts as undefined, so if you read it before its line you get undefined, not an error.
  3. let can be reassigned but not redeclared in the same scope. It's hoisted but not initialized, so reading it before the declaration throws a ReferenceError.
  4. const can't be reassigned or redeclared, and you have to give it a value on the line where you declare it.
  5. The catch with const is that it fixes the binding, not the value, so if it holds an object you can still change that object's properties.

Weak answers to avoid

  • Says const makes the value immutable const only blocks reassigning the name. An object or array held in a const can still be changed, so say 'fixed binding' and mention Object.freeze if asked.
  • Claims let and const are not hoisted They are hoisted but left uninitialized until their line. That's why an early read throws a ReferenceError instead of reading an outer variable with the same name.
  • Talks about reassignment and skips scope Scope is the main difference. Show that a var declared inside an if or a loop is still visible after the block, while let and const are not.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime