fun() // **Arrow Function (fun) is called before function definition**
var fun = () => {
console.log("xdssdyz");
}
// How it predicts that fun is not a function , according to execution context and function hoisting .
fun() // **Arrow Function (fun) is called before function definition**
var fun = () => {
console.log("xdssdyz");
}
// How it predicts that fun is not a function , according to execution context and function hoisting .
With hoisting var
, the fun
variable will be undefined
before the declaration, and you try to execute undefined
as a function
, that's why it gives an error `Uncaught TypeError: fun is not a function
arrow function isn't hoisted in javascript. An arrow function is not actually a function: it is an expression of a function and expressions is not hoisted in javascript. Basically an anonymous function is kept in a variable of arrow function instead of declaring it. That's why such thing happens. If you want your function to be hoisted, declare it with function
keyword, that's how js understands it as a function