Join the DZone community and get the full member experience.Join For Free
On the clickbait talk title, that’s actually a true story. Last time I mentioned the hoisting term in an article of mine, I remember that a friend read it and told me I had a typo in the article.
When declaring a variable, only its name gets hoisted.
Here’s how the above is interpreted, thus
var, a function declaration hoists its actual definition too.
Here’s how the above is interpreted, which results in
The variable name gets hoisted, but not the function assignment.
Here’s how the above is interpreted,which results in
hello is not a function:
On ES6 and Later
The hoisting behavior isn’t something that applies to the latest ECMAScript gems:
- class declaration
- arrow function
Last but Not Least
Here’s what Brendan Eich said about JS hoisting:
varhoisting was an implementation artifact. - Source
functionhoisting was better motivated: function declaration hoisting is for mutual recursion & generally to avoid painful bottom-up ML-like order - Source
Here are the slides, just in case.
Published at DZone with permission of Catalin Red, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.