Just out of nowhere, why hoisting isn't a good practice?


I know this is an irrelevant question, but why is ‘hoisting’ a good practice if we just want to call the function?

Hey, @denzelsugayan6884482!

I wouldn’t say that hoisting isn’t a good practice. But, before I explain ‘why’ I think that, you might want to read the MDN docs about hoisting. Here’s the link to it: Hoisting - MDN Web Docs Glossary: Definitions of Web-related terms | MDN

I think hoisting is an excellent feature of JavaScript but we still have to be very careful with how we use this feature. I like to put all the executable code of a file at the top and all the function declarations below this executable code which helps me on readability and also, everytime I open the file, the main part of code is right there in front of me and I don’t have to scroll to the bottom looking for it everytime. I’m using the functionality of JS to hoist function declarations to gain better readability and reduce scrolling. But as I said before, you gotta be careful with hoisting since as far as functions are concerned, JS only hoists function declarations and as far as variables are concerned, only the variables declared with var and they have to be initialised. Keep this in mind and you’ll be fine.

To sum it up, hoisting is available to us in JavaScript as a tool whether we think it’s a great idea or not and we, as developers, should be the one to decide if using it helps us write more clear and readable code or not.


I kinda see where you explain hoisting’s job in Javascript. I guess I didn’t know at first why it isn’t considered a ‘good practice’. I will keep your advice to properly use hoisting as intended to only variables declared with the var

thanks again


I hope you understand the difference between function declarations and function expressions since JS will only hoist function declarations and not any other type of functions. You might want to read more about this.