r/learnjavascript • • Jul 19 '21

JavaScript Function Facts

255 Upvotes

27 comments sorted by

View all comments

Show parent comments

3

u/Claudioub16 Jul 19 '21

I'm new to programming, but I really dislike hoisting. Why would you want to hoist a function?

7

u/toastertop Jul 19 '21

Functions called before they are declared, won't throw a undefined error.

3

u/Claudioub16 Jul 19 '21

Since I'm novice allow me to ask this question: wouldn't be a easy fix to just put the function before the call?

7

u/toastertop Jul 19 '21

There's nothing to "fix" per say, as this is what hoisting is doing for you during the compilation stage of execution. Specifically moving function definitions into memory.

However, I'd say it's best practice to declare functions, before calling as it's easier to reason about the code and reads better in a top down approach. But the JavaScript compiler could care less.