JavaScript for WordPress Forums Vanilla JavaScript 1.2.20 – Introduction to Functions – Hoisting Reply To: 1.2.20 – Introduction to Functions – Hoisting

#26810
Zac Gordon
Keymaster

Great point Jeremy!

We are going to get into scoping and hoisting in our advanced JS topics, but you are right on point there and it’s a good thing to mention about seeing it in other people’s code (even some of my examples in this course).

I couldn’t really find a concensus on plain function declaration v expression and variable assignment, but if using the former it’s best to wrap your code in an IIFE to avoid it being hosted all the way to global scope.

It’s kind of similar to what happens when you assign a variable without using var, which is why I felt the named function expression was a little “stricter” in some way so possibly better to encourage.

Since making those videos I have talked with folks who say just regular function declarations are fine if in an IIFE.

Thanks for bringing up this whole topic and good point that I didn’t mention in the videos. Looking forward to digging into this deeper very soon 🙂