What is the Temporal Dead Zone?

Mar 21, 2018

The first time I heard about the “temporal dead zone” I thought perhaps we had left javascript land and were talking about some type of shrimp tempura. Indeed, I was wrong. I must have been hungry at the time.

The temporal dead zone or “tdz” for short, is relevant when writing es6 with let and const. Because let and const are block scoped and because they cannot be accessed before they are declared, you can run into situations where let and const are not defined. 

Let’s take a look at some examples using our tempura theme:

var favoriteRoll = “Salmon Avocado”;

What do you think will be printed to the console?

“Salmon Avocado” as we expected. Let’s take a look at the outcome when we switch the order. 

var favoriteRoll = “Salmon Avocado”;

What will this give us?

Since variables are hoisted in javascript, favoriteRoll’s declaration and not initialization is hoisted to the top of the scope. Check out MDN’s section on var hoisting in case you want to learn more. 

Now let’s try our new friends let and const. 

let favoriteSauce = “soy sauce”;
const WESTCOASTROLL = “California Roll”;

What will this produce?

“soy sauce”
“California Roll” are correct. 

Let’s switch up the order. 

let favoriteSauce = “soy sauce”;
const WESTCOASTROLL = “California Roll”;

What does this produce?

It’s not undefined which we got when we used var.
We get “Uncaught ReferenceError: favoriteSauce is not defined”.

Why is this happening?

Because unlike var both let and const cannot be accessed before they are declared. 
MDN provides a good explanation of let and const: “In ECMAScript 2015…. Referencing the variable in the block before the initialization results in a ReferenceError (contrary to a variable declared with var, which will just have the undefined value). The variable is in a ‘temporal dead zone’ from the start of the block until the initialization is processed.”

It’s good to be cognizant of the temporal dead zone when writing code with es6. To help prevent against tdz, it is recommended to declare variables (let / const) at the top of the scope.

Senior, Lead, or Principal developer in NYC? Stride is hiring! Want to level up your tech team? See how we do it! www.stridenyc.com

You May Also Like

These Stories On Technology

Josh Seiden, the fourth speaker in Stride's Leadership through adversity speaker series, spoke about the value of focusing on outcomes, defined as: "measurable changes in behavior that drive business results."   read more

To start his talk, psychiatrist and psychoanalyst Dr. Kerry Sulkowicz admitted to being at a bit of a loss when asked about best practices for leading through a pandemic. "The honest answer is I don't know," he stated, "because none of us has lived ... read more

Taking direct aim at the narrative of the "all in" entrepreneur who takes extreme risks and depletes their bank account before ultimately succeeding, noted NYC VC Charlie O'Donnell started his Stride Consulting “Leading through Adversity” talk on May ... read more

In response to these unprecedented times and the challenges that they are bringing to company leaders, Stride Consulting launched a speaker series entitled “Leading through Adversity” on May 26th. In the series, experts from around the globe, from a ... read more

Get Email Updates