Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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ā;console.log(favoriteRoll);
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.
console.log(favoriteRoll); var favoriteRoll = āSalmon Avocadoā;
What will this give us?undefinedSince 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ā;console.log(favoriteSauce);console.log(WESTCOASTROLL);
What will this produce?āsoy sauceāāCalifornia Rollā areĀ correct.
Letās switch up theĀ order.
console.log(favoriteSauce);console.log(WESTCOASTROLL);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
Originally posted on the Stride Blog. Author: LindaĀ Gonzalez
What is the Temporal Dead Zone? was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.