Hoisting is a JavaScript-specific behavior that moves variable and function declarations to the top of their scope during the compile phase. This means that you can use variables and functions before theyβre declared in your code. While this might sound convenient, it can also lead to confusion and bugs if you're not careful.
In JavaScript, there are two main types of hoisting:
- Variable Hoisting
Variables declared withvar
are hoisted to the top of their scope, but only their declarations are hoisted. The initialization (assignment of a value) remains in place. This can lead to unexpected results.