Hoisting in JavaScript: Most Asked Topic in JOb Interviews

dev_abdulhaseeb

Abdul Haseeb

Posted on January 19, 2024

Hoisting in JavaScript: Most Asked Topic in JOb Interviews

Image description

no trash talking let's get straight in

look at this example:

see function is called before it is declared,
but JavaScript automatically puts the declaration at the top of the script this is called hoisting it is simple:

Image description

remember that only declaration will be hoisted not the initialization.

var myVariable will be hoisted and it contains the value undefined after assignment it contains the value 42
so here "var myVariable" is hoisted nut "= 42" is not.

Image description

here when we declare the function using var you can see that calling sayHello() before intialization throws an error because intialization is not hoisted "Var sayHello" existed but we did'nt assigned a function to it as initialization is not hoisted so it throws an error.

Image description

💖 💪 🙅 🚩
dev_abdulhaseeb
Abdul Haseeb

Posted on January 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024