Search
Try Notion
🔑🔑
Hoisting
We know that JavaScript runs top to bottom. Lets assume a scenario!
Scenario :
Lets create a function namely test and console log it!
function test() { return 1+2; } console.log(test()); //output -> 3
JavaScript
Now lets assume what will happen if we log the function first then initialize it?
console.log(test()); function test() { return 1+2; }
JavaScript
//output -> 3
JavaScript
It will run same as the first method! Why? because hoisting moves some part of the code like functions and vars to top of the script file!
Q. What will happen if we use arrow function ?
Ans → well hoisting only works for normal functions with function keyword and vars!
console.log(test()); const test = () => { return 1+2; }
JavaScript
//output -> Error test() must be initialize first!
JavaScript