Overview - What hoisting is
What is it?
Hoisting is a behavior in JavaScript where variable and function declarations are moved to the top of their containing scope before the code runs. This means you can use variables and functions before you write them in your code. However, only the declarations are hoisted, not the initial values or assignments.
Why it matters
Hoisting exists to make JavaScript code more flexible and forgiving, allowing functions and variables to be used before they appear in the code. Without hoisting, you would have to declare everything before using it, which can make code less readable and harder to organize. Understanding hoisting helps avoid confusing bugs where variables seem to exist but have unexpected values.
Where it fits
Before learning hoisting, you should understand basic JavaScript syntax, variables, and functions. After hoisting, you can learn about scopes, closures, and asynchronous JavaScript, which build on how JavaScript handles variables and functions.