Ajay Yadav
Ajay Yadav

@ATechAjay

11 Tweets 11 reads May 30, 2022
🀯 Under the hood of the JavaScript series!
5️⃣ HOISTING & TDZ(Temporal Dead Zone) in JavaScript?
A ThreadπŸ§΅πŸ‘‡
β›” Before going deep dive into this topic there is a prerequisite for this topic which is "scope" in javascript.
πŸ‘€ You can learn it from hereπŸ‘‡
πŸ“Œ What is HOISTING?
β†’ HOISTING is a JavaScript behavior in which a function or variable can be accessed or used before the declaration.
β†’ Or, a JavaScript variable/function can be used before being declared.
β†’ But it is not going to work with let and const keywords.
β†’ Hoisting only works with the "var" keyword.
β†’ Javascript "initialization" is not hoisted but "declarations" are hoisted.
β†’ See in this example, the only declaration is hoisted not the value.
β†’ Variable declared with "var" keyword is initialized with "undefined" value by default.
β†’ The function is also hoisted with only the "var" keyword but not initialized.
β†’ But it will not work with "let" and "const" keywords.
β†’ You will get a ReferenceError because of TDZ(Temporal Dead Zone).
β›” let and const are hoisted but not initialized with a default value like the "var" keyword that's why it gives an error.
πŸ‘€ Here is some example of the function.
β›” In the arrow function, we get an error as "sumArrow is not a function" because the value of the "sumArrow" function is undefined.
β›” So we can't call like "undefined(10, 20)"
πŸ“Œ What is TDZ(Temporal Dead Zone)?
β†’ That means we can not access a variable or function before its declaration.
β†’ This works only with the "let" and "const" keywords.
β†’ An area or zone which is temporarily inactive for a particular variable or function.
β†’ The above image shows that the red box is in the "temporal dead zone" of the "TDZ" variable.
β†’ That means it can be accessible before line number 6.
β›” It will be accessible starting from the line where it is defined.
β›” Otherwise, we will get a ReferenceError.
That's all for now, we will meet in the next thread😍
But if you like make sure to:
1. Follow me @ATechAjay
2. Retweet the first tweet.
3. Turn on the notification to never miss these amazing tweets.
Thank you so much for staying to the end of this thread.

Loading suggestions...