What is Hoisting😰😰 in JavaScript

Sai gowtham on April 28, 2018

Hoisting is not something functions and variables are moving to the top. Actually, they are not moving technically anywhere. The thing happens a... [Read Full]
markdown guide

Thanks for the write up Sai.

May I just share something that can help a bit more with this topic:

We can be tempted to look at var a = 2; as one statement, but the JavaScript Engine does not see it that way. It sees var a and a = 2 as two separate statements, the first one a compiler-phase task, and the second one an execution-phase task.

What this leads to is that all declarations in a scope, regardless of where they appear, are processed first before the code itself is executed. You can visualize this as declarations (variables and functions) being "moved" to the top of their respective scopes, which we call "hoisting".

Declarations themselves are hoisted, but assignments, even assignments of function expressions, are not hoisted.

From You Don't Know JS

strongly recommend the whole series


Yeah sure I have also seen from other books(JavaScript for ninjas). Check out JavaScript weird parts good explanation about hoisting.


It moves the declaration for var up but not the value set

var value;
console.log('hi hoisting');
value = 'hoisting';


Variable declaration and assignment are done separately.


just a figure of speech :-) I mean it's interesting that you both were "struggling" with this behavior of JS at the same time and wrote about it.

That’s something that all Javascript developer should know... so more points of view... are better 💛

code of conduct - report abuse