All 28 lessons ▾

Inside JavaScript · Lesson 8 of 28

let, const and the Temporal Dead Zone

Updated 11 Nov 2026 · By Akshay, software engineer, India · 7 min read

So, are let and const hoisted? Most people say no. And they're wrong! Now the real answer comes with a scary name, the temporal dead zone. So let's understand it properly.

The video for this lesson is coming soon.

A new lesson goes up every day at 11 am. Subscribe on YouTube so you don't miss it.

Got a doubt? Ask in the comments under the video.Subscribe

In one minute
  • let and const are hoisted, but locked
  • Temporal dead zone: from hoisting until the value is set
  • Using them there: ReferenceError
  • They live in a separate memory, not on window
  • No redeclaring; const needs a value and can't change
  • const by default, let when it changes, avoid var

Part 1Are let and const hoisted?

So far in this course, we've used var everywhere. But in modern JavaScript, you'll mostly write let and const. And they behave differently from var, especially with hoisting. Now this is a very popular interview topic, so listen to this very carefully. Let's start with code, as always.

shop.js
1console.log(count);2console.log(price);3 4var count = 3;5let price = 99;
Output
undefined
ReferenceError: Cannot access 'price' before initialization

So here's a small program. count is a var, and price is a let. Phase one runs first. count gets undefined, just like we've seen many times. But price? Watch this. price is also given memory, but in a separate place, and it's locked. It's not ready to be used yet. Now phase two. Line one logs count, and it prints undefined. No surprise there. Line two logs price, and boom, an error. A reference error. Cannot access price before initialization. Now notice the message. It doesn't say price is not defined. It says you can't access it yet. That's a big clue. JavaScript knows price exists!

Global memory
var count, functions
Script memory
let price, const values
also on window
locked until its line
REMEMBER THIS
letandconstARE
hoisted,butkeptlocked
untiltheirlineruns.
so they're not usable early

So remember this. let and const are hoisted. Yes, they are! JavaScript reserves memory for them in phase one, just like var. Now the difference is, they're not given undefined in the normal memory. They're kept in a separate place, and they stay locked until the line that declares them actually runs. So they are hoisted, but you can't use them early.

Now if you open your browser's developer tools and pause at line one, you can actually see this. So var count sits in the global memory, the same place that goes on window. But price sits in a separate section, which the browser calls script. A different memory space, made just for let and const. And price stays locked in there, until its line runs. So now you know exactly where they live.

Part 2The temporal dead zone

Now, the time when price is locked has a name. And it's a scary one.

REMEMBER THIS
Temporaldeadzone:fromhoisting
untilthevariablegetsitsvalue.
touch it there and you get a ReferenceError

It's called the temporal dead zone. Sounds like a horror movie, doesn't it? But it's simple. Temporal means time. So the temporal dead zone is a stretch of time. It starts when the let variable is hoisted, and it ends when the variable gets its value. Now during that time, the variable is dead to you. You can't read it, you can't use it. So if you touch it, you get a reference error.

tdz.js
1// price is in its dead zone here2// still in the dead zone here3let price = 99;4console.log(price);
Output
99

So let's see the dead zone on real code. The dead zone starts at the very top, right after hoisting. Line one, still dead. Line two, still dead. Then line three, let price equals ninety nine. This is where the dead zone ends. price finally gets its value, and it's unlocked. Line four logs price, and it prints ninety nine. So every line before the declaration is the dead zone for price.

window.js
1var count = 3;2let price = 99;3 4console.log(window.count);5console.log(window.price);
Output
3
undefined

And now remember from episode five, global var variables go on window? So let's check let. Line four logs window.count, and it prints three. As expected. Line five logs window.price, and it prints undefined. Because price never went on window. It lives in that separate script memory. So let and const don't go on the global object. That's one more difference from var. And since this is window at the top level, this.price is undefined too.

BUT WHY?
Thedeadzoneturnsasilentbug
intoalouderror.
var hides the mistake with undefined

Now you might ask, why did JavaScript even add this dead zone? Wasn't undefined good enough? So here's the reason. Using a variable before it's set is almost always a mistake. With var, that mistake is silent. You just get undefined, and maybe a NaN somewhere later, like we saw in the hoisting episode, and you spend an hour hunting for it. But with let and const, the same mistake is loud. You get an error, on the exact line, right away. So the dead zone isn't there to annoy you. It's there to protect you.

Part 3let and const are stricter

Now, let and const aren't just different, they're stricter than var. And that's a good thing. So watch.

duplicate.js
1let level = 1;2let level = 2;
Output
SyntaxError: Identifier 'level' has already been declared

So with var, you can declare the same name twice, and JavaScript quietly allows it. But with let, it's not allowed. Now look at this. let level, and then let level again. JavaScript rejects this whole file, before running a single line. A syntax error. Identifier level has already been declared. Not even line one runs. And the same happens if you mix them, a let and a var with the same name, in the same scope. Syntax error again.

const.js
1let score;2score = 10;3 4const max = 100;5max = 200;
Output
TypeError: Assignment to constant variable.

And now const is even stricter than let. With let, you can declare a variable on one line, and give it a value later, like score here. That's fine. But a const must get its value on the same line. const max equals one hundred. And once it has a value, it can never be reassigned. Line five tries to set max to two hundred. And we get a type error. Assignment to constant variable. And if you write const with no value at all, that's a syntax error, missing initializer. So a const is a promise. This value won't change.

Three kinds of errors
->
ReferenceError: not defined, or used in its dead zone
->
SyntaxError: the code is rejected before it runs
->
TypeError: the wrong kind of operation, like changing a const

So today we met three kinds of errors. Let's line them up, because interviewers love asking this. A reference error means a variable can't be reached. Either it's not defined anywhere, or it's in its temporal dead zone. A syntax error means your code is rejected before it even starts, like declaring the same let twice. And a type error means you tried the wrong kind of operation, like changing a const. So three errors, three different meanings.

Part 4So what should you use?

So with var, let and const, which one should you actually use in your code? Let's see.

GOOD PRACTICE
Useconstbydefault,let
whenitchanges,and
avoidvar.
declare at the top, so the dead zone shrinks to zero
INTERVIEW ANSWER
Yes,letandconstare
hoisted,butstayinthe
temporaldeadzoneuntil
initialized.
not on window, no redeclaring, const can't be
reassigned

So here's the simple rule most good developers follow. Use const by default, for everything that won't change. If a value needs to change, use let. And avoid var in new code. Now one more tip. Declare and initialize your variables at the top of their block. That way, the temporal dead zone shrinks to almost nothing, and you'll never hit that error by accident. Simple. So try it from today.

Quick quiz
quiz.js
1console.log(a);2var a = 1;3 4console.log(b);5let b = 2;

What does line one print?

Show the answer

So a is a var, so it's undefined. And line four? b is a let, still in its temporal dead zone. So that's a reference error, cannot access b before initialization. Not undefined! If you got that difference, you've completely nailed today's episode. Well done!

Your turn

Which one do you use the most, let or const?

Answer in the comments under the video.

Your progress stays on this device.
A new lesson every day at 11 am

The video on YouTube, and the lesson as a swipe-through on Instagram.