Javascript Hoisting: Thank you for nothing

Krishna Singh
JavaScript in Plain English
3 min readJan 14, 2023

--

Photo by <a href=”https://unsplash.com/@blakeconnally?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Blake Connally</a> on <a href=”https://unsplash.com/photos/B3l0g6HLxr8?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
Photo by Blake Connally on Unsplash

As a JavaScript developer, I’m sure you’re well aware of the “joy” that is hoisting. But in case you need a refresher, hoisting is the behavior in JavaScript where variables and functions are automatically moved to the top of their scope before code execution.

Now, some might argue that hoisting is a useful feature, allowing us to reference variables and functions before they are actually declared in our code. But let’s be real here: the cons far outweigh the pros when it comes to this “feature.”

One of the main issues with hoisting is that it can be extremely confusing for new developers. When you’re learning to code, it’s important to have a solid understanding of the concepts and principles you’re working with. Hoisting goes against some of the fundamental rules of programming, such as the idea that declarations should come before use and that variables should be initialized before they are used. This can lead to confusion and can make it more difficult for new developers to understand what’s going on in their code.

But even for experienced developers, hoisting can be a nuisance. It’s hard enough trying to keep track of all the variables and functions in your code, let alone worrying about their placement in relation to hoisting. This can lead to wasted time and frustration as you try to debug your code and figure out why it’s not working as expected.

In addition to being confusing and frustrating, hoisting is also inconsistent. As mentioned before, it behaves differently depending on whether you’re using strict mode or not. This means that you have to be extra careful about how you use hoisting, and you may have to change your code depending on whether you’re using strict mode or not. This adds another layer of complexity to an already confusing concept and can make it more difficult to write reliable, predictable code.

Finally, it’s worth noting that the supposed “pros” of hoisting are really quite minimal. Yes, it allows us to reference variables and functions before they are declared in our code, but is this really that useful? In most cases, it’s much clearer and easier to read code if declarations are made before they are used. This helps to avoid confusion and makes it easier to understand what’s going on in the code.

So, in conclusion, hoisting is a confusing and frustrating aspect of JavaScript that serves little purpose and only serves to make our lives as developers more difficult. Sure, it may have a few minor benefits, but the cons far outweigh them. Thank you for nothing, hoisting.

You’re building your own maze, in a way, and you might just get lost in it.
― Marijn Haverbeke, Eloquent JavaScript: A Modern Introduction to Programming

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Interested in scaling your software startup? Check out Circuit.

--

--

Software Engineer with proficiency in PHP/Drupal and a strong interest in JavaScript.