Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6061

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T03:03:08+00:00 2024-11-27T03:03:08+00:00

Hoisting and Temporal Dead Zone in Javascript

  • 60k

Before getting into the details of hoisting and temporal dead zone, Let's see a code snippet to see the pitfalls of hoisting. So that we can handle it in a better way. In the below code snippet we trying to clear the cart when the noOfProducts is empty

if(!noOfProducts) clearCart() var noOfProducts = 5; function clearCart(){     console.log("All products are deleted") } //Output is: All products are deleted 
Enter fullscreen mode Exit fullscreen mode

But when you run this code you will see the clearCart function will be executed and the console log will be printed All products are deleted. This happens because of hoisting. Now let's see about hoisting and the temporal dead zone in this blog.

What is Hoisting?

Hoisting is nothing but making some types of variables accessible/usable in the code before they are
actually declared. Let's see in a detailed way.

In javascript, the execution takes place in two phases,

  • Memory creation phase
  • Code execution phase

In the memory execution phase, every variable and function is assigned to memory with some values for it. This phenomenon is called hoisting. Let's dig deep and see what is that some value for each in the case of variables and functions.

Hoisting in Var

When you declare a variable with var,

  • In the memory creation phase the special keyword called undefined will be assigned to the variable.
  • so when you try to access the variable before the declaration undefined will be printed and after the initialization, the value is printed
  • If we fail to initialize the variable in the whole program, then the variable will have the value as undefined till the end of the program.

Let's see with an example code.

console.log(a) // undefined console.log(b) //undefined var a=10; var b; console.log(a) // 10 console.log(b) //undefined 
Enter fullscreen mode Exit fullscreen mode

Hoisting with function declaration

In the case of function declaration,

  • In the memory creation phase, the memory for the function is allocated with the whole code in the function.
  • This makes the ability to call the function before its declaration in javascript.

Let's see with an example code.

console.log(printName)  //function printName { //   console.log("Manikandan"); //} console.log(printName()) //Manikandan function printName(){    console.log("Manikandan"); }  
Enter fullscreen mode Exit fullscreen mode

Temporal dead zone

In the case of a variable declared with const and let,

  • These variables are hoisted but can't be accessed by us until the declaration happens.
  • When we try to access these variables we will get the error ReferenceError: Cannot access 'variableName' before initialization
  • The place in between where we try to access the variable before its declaration and till the line of declaration is said to be that the variable is in a temporal dead zone.

Let's see with an example code.

1. console.log(a) //ReferenceError: Cannot access 'a' before initialization 2. console.log(b) //ReferenceError: Cannot access 'b' before initialization 3. let a =10; 4. const b=20; 5. console.log(a) //10 6. console.log(b) //20  
Enter fullscreen mode Exit fullscreen mode

  • The place in between line 1 and line 3 is said as a temporal dead zone of the variable a
  • The place in between line 2 and line 4 is said as a temporal dead zone of the variable b

Hoisting with a function expression and arrow functions

Hoisting with a function expression and arrow function happens based on the variable type it is assigned to,

  • If functions are assigned to var and if we try to access the function before declaration we get undefined
  • If functions are assigned to let and const and if we try to access the function before declaration we get an error and it will be in the Temporal dead zone.

Let's see with an example code.

//Function expression console.log(printFirstName) //undefined printFirstName() //TypeError : printFirstName is not a function var printFirstName = function (){ console.log("Manikandan") } printFirstName() //Manikandan  //Arrow function console.log(printLastName) //ReferenceError: Cannot access 'printLastName' before initialization const printLastName= ()=>{ console.log("Subramaniam") } printLastName() //Subramaniam 
Enter fullscreen mode Exit fullscreen mode

Practice to be followed to avoid hoisting pitfalls

As we saw initially hoisting can cause some serious issues if not handled properly to avoid that,

  • Access variables only after their initialization
  • Call the function only after its declaration
  • If Both function and variable need to be accessed before then handle with caution

Conclusion

Summary

  • Hoisting is nothing but making some types of variables accessible/usable in the code before they are actually declared
  • Hoisting Summary table
Hoisted Initial value
function declarations yes actually function
var variables yes undefined
let and const variables yes TDZ (temporal dead zone)
function expression and arrow functions Based on variables it assigned to var/let,const

Resourses

  • MDN Hoisting

beginnershoistingjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.