BY Simmi Kava27 Jul 2021 Edit
JavaScript: Temporal Dead Zone (TDZ)

What is Temporal Dead Zone (TDZ)?


TDZ is a term to describe the state of variables where they are not accessible. A variable declared using let & const is in TDZ from the start of the block until the initialization has completed.

A variable defined using let cannot be read/written until they have been full initialized. If no initial value provided, defaults to undefined.

Whereas, variables declared using the keyword var will return a value of undefined if accessed before declaration.

{ //TDZ starts at beginning of scope
console.log(framework); // Returns: undefined
console.log(ranking); // Returns: ReferenceError
var framework = "WebAtoms";
let ranking = "#1"; //TDZ ends here (for ranking)
}

The term "temporal" is used because the zone depends on the time it is executed (order of execution) rather than position(order) where it is declared.

Q) What will happen if we use typeOf operator on a variable which is in TDZ?

A: It will throw a Reference Error. But when a typeOf operator is used on a undeclared or undefined variable, no error is thrown.

MDN Reference Link

Have a nice day!

BY Simmi Kava
LikeCommentSave
LikeCommentSaveShare
0

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
srikanth.naidu
liked this post.
malli
saved this post.
Show more
ARCHIVES
2021
2020
2019
2018
TAGS
javascript (41)
developer (25)
javascriptdeveloper (15)
coding (10)
Xamarin.Forms (10)
webatoms (8)
arrays (7)
xamarin (7)
android (6)
typescript (6)
function (5)
csharp (4)
iOS (4)
methods (4)
object (4)
update (4)
web-atoms (4)
arrayoperation (3)
decorators (3)
programming (3)
prototype (3)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository