DEV Community

Cover image for Javascript Hoisting

Javascript Hoisting

Naveenchandar on July 06, 2021

Hoisting is one of the important concepts every javascript or its related frameworks developers should be familiar with. Ever wonder how function c...
Collapse
 
copleykj profile image
Kelly Copley

"Let and const are hoisted in block scope whereas var is hoisted in global scope."

Actually var declarations are hoisted in function scope, and variables that are initialized without var, let or const are hoisted globally.

Collapse
 
naveenchandar profile image
Naveenchandar

Yep missed that one. That depends on the declaration. If that's inside particular block, it'll not get hoisted globally and if not, Let or const will be hoisted globally but due to temporal dead zone we can't access those until initialization.
"Let and const are hoisted in block scope whereas var is hoisted in global scope." -> Whenever we try to re declare a same variable using var inside a specific block, variable will get updated and that's not the same case with let because it is block scope so it will not be available outside the scope.
Example:

let greeting = "Hello";
    if (true) {
        let greeting = "Hi";
        console.log(greeting); // "Hi"
    }
    console.log(greeting); // "Hello"
Enter fullscreen mode Exit fullscreen mode
var greeting = "Hi";
    if (true) {
        var greeting= "Hello!";
        console.log(greeting); // "Hello!"
    }
    console.log(greeting); // "Hello"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
christianbarnabechabi profile image
Christian Barnabé

Not sure about this

Collapse
 
copleykj profile image
Kelly Copley

You mean you were unsure about this, but then you read my comment. Now you know and are no longer unsure 😉

Collapse
 
robencom profile image
robencom

so in a nutshell: only variable declarations are hoisted (elevated to the top of the code) to not throw any ERRORS, but of course these variables are undefined; but when it comes to functions, if they are not arrow functions or function expressions, then the function is TOTALLY hoisted.

Collapse
 
naveenchandar profile image
Naveenchandar

Exactly.

Collapse
 
robencom profile image
robencom

Great article really. I was recently asked this question in a interview, I blabbered something and even the interviewer didn't understand what hoisting really is, so my answer passed :D but now I know what to say.. MANY THANKS!

Thread Thread
 
naveenchandar profile image
Naveenchandar

😊😊😊.

Collapse
 
copleykj profile image
Kelly Copley

This is because all declarations are hoisted. Function declarations are no different than variable declarations, memory must be reserved for them as well.

Collapse
 
smlka profile image
Andrey Smolko

you said about TDZ:
"It is Time taken between declaring the variable(using let or const) and initializing the variable."
IMHO It will be super useful to mention when exactly let/const variables declaring/creating. The answer - when new scope is instantiated. So TDZ always starts from the first line of the scope and ends at line with let/const a =...

and like for attempt describe hoisting for let/const=)

Collapse
 
naveenchandar profile image
Naveenchandar

I should've elaborated that part but thanks 🙂.

Collapse
 
faabiopontes profile image
Fabio Pontes

What's up, very nice article. Didn't know about the high priority, JavaScript always with it's surprises.

There's a typo in "Javascript will always go through these lifecycles to delcare a variable"

Collapse
 
naveenchandar profile image
Naveenchandar

Thanks for notifying, its was by mistake.
Fixed it 🙏🏼

Collapse
 
meyashtiwari profile image
Yash Tiwari

Very informative and helpful.

Collapse
 
naveenchandar profile image
Naveenchandar

Thank you..😊

Collapse
 
arvindpdmn profile image
Arvind Padmanabhan

From the comments below, there seems to be confusion about var, let and const in terms of hoisting. See devopedia.org/hoisting

Collapse
 
tanth1993 profile image
tanth1993

thank you for your sharing. now I know more about hoisting

Collapse
 
naveenchandar profile image
Naveenchandar

🙂.

Collapse
 
rexgalilae profile image
RexGalilae

Stale memes but decent explanation

Collapse
 
naveenchandar profile image
Naveenchandar

Thank you 🙂.