Sushrut Mishra
Sushrut Mishra

@SushrutKM

9 Tweets Apr 14, 2023
Super Simple Explanation: What is Hoisting in JavaScript?
🧵⬇️
In short,
➡️ Other Programming languages -
First Declare, then use.
➡️ JavaScript -
You want to use it first? Okay! Declare later.
➡️ In JavaScript, a variable can be used before it has been declared. This is termed as "Hoisting"
Hoisting can also be understood as a behavior of JS where it automatically moves all declarations to the top of the current scope.
➡️Variables declared outside of a block are Global variables.
Example -
➡️Local variables are those declared inside of a block.
Example -
➡️ Hoisting with Example -
➡️ Point to remember -
You can not hoist initializations like you hoist the declaration.
Like in the above example-
x was initialized with value 5 and was declared later.
But if you use a variable before initializing it, you'd get an error.
Example -
➡️ The concept of Hoisting in Javascript is super-important and a bit confusing at the same time.
However, there is no such need to declare later in the name of Hoisting. It is easier to code when you Declare at the top.
That's a wrap!
If you enjoyed this thread:
1. Follow me @SushrutKM for more of these
2. RT the tweet below to share this thread with your audience

Loading suggestions...