Are you confused how hoisting works in JavaScript?
It confused everyone in the starting. So, don't worry about it. I will help you understand how hoisting works in JavaScript through this article.
So, let’s start with this :
JavaScript hoisting is the process where the interpreter appears to move the declaration of a function, variable or classes to the top of their scope, prior to execution of the code.
in simple words
In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared.
Function and variables can be use before variable.
only "var" allow hoisting in javascript whereas "let" and "const" do not allow hoisting.
greet();
function greet(){
console.log("Hello user")
}
Output: Hello user
Here we run a function greet() before initialise it and it's working.