Hoisting is one of the important concepts every javascript or its related frameworks developers should be familiar with. Ever wonder how function c...
For further actions, you may consider blocking this person and/or reporting abuse
"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.
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
orconst
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 withlet
because it is block scope so it will not be available outside the scope.Example:
Not sure about this
You mean you were unsure about this, but then you read my comment. Now you know and are no longer unsure 😉
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.
Exactly.
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!
😊😊😊.
This is because all declarations are hoisted. Function declarations are no different than variable declarations, memory must be reserved for them as well.
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=)
I should've elaborated that part but thanks 🙂.
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"
Thanks for notifying, its was by mistake.
Fixed it 🙏🏼
Very informative and helpful.
Thank you..😊
From the comments below, there seems to be confusion about var, let and const in terms of hoisting. See devopedia.org/hoisting
Stale memes but decent explanation
Thank you 🙂.
thank you for your sharing. now I know more about hoisting
🙂.