DEV Community

Abdul Haseeb
Abdul Haseeb

Posted on

2 1 1 1 1

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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more