-
"In JavaScript,
hoisting
is behavior where we can use variables and functions before they are declared in our code."However, it's important to note that
only the declarations
are hoisted,not the initializations
. If you initialize a variable after using it, the value will beundefined
until the assignment statement is executed.
-
Variable Hoisting:
console.log(myVariable); // undefined var myVariable = 10; console.log(myVariable); // 10
-
Function Hoisting:
sayHello(); // "Hello" function sayHello() { console.log("Hello"); }
-
Function Expression Hoisting:
sayHello(); // TypeError: sayHello is not a function var sayHello = function() { console.log("Hello"); };
- The TDZ (
Temporal Dead Zone
) error occurs when we try to access or use variables declared withlet
andconst
before they are formally declared or initialized. In other words, we cannot access these variables before theirdeclaration
orinitialization
in our code, or we will encounter aTDZ
error.
-
Example 1:
console.log(myVariable); // ReferenceError: Cannot access 'myVariable' before initialization const myVariable = 10;
-
Example 2:
function example() { if (true) { console.log(myVariable); // ReferenceError: Cannot access 'myVariable' before initialization let myVariable = 20; } } example();
-
Example 3:
function example(param = myVariable) { // ReferenceError: Cannot access 'myVariable' before initialization let myVariable = 30; console.log(param); } example();
-
Bonus Example:
{ // bestFood’s TDZ starts here (at the beginning of this block’s local scope) // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here console.log(bestFood); // returns ReferenceError because bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here let bestFood = "Vegetable Fried Rice"; // bestFood’s TDZ ends here // bestFood’s TDZ does not exist here // bestFood’s TDZ does not exist here // bestFood’s TDZ does not exist here }
{ // TDZ starts here (at the beginning of this block’s local scope) // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here let bestFood = "Vegetable Fried Rice"; // bestFood’s TDZ ends here console.log(bestFood); // returns "Vegetable Fried Rice" because bestFood’s TDZ does not exist here // bestFood’s TDZ does not exist here // bestFood’s TDZ does not exist here }
{ // TDZ starts here (at the beginning of this block’s local scope) // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here // bestFood’s TDZ continues here let bestFood; // bestFood’s TDZ ends here console.log(bestFood); // returns undefined because bestFood’s TDZ does not exist here bestFood = "Vegetable Fried Rice"; // bestFood’s TDZ does not exist here console.log(bestFood); // returns "Vegetable Fried Rice" because bestFood’s TDZ does not exist here }