There are three ways a JavaScript function can be created. They are as follows:
- Function declaration
- Function expression
- Arrow function
When you create a function as a declaration or statement, it gets hoisted at the top of the execution context and can be used before it is made. Example below:
console.log(Foo());
function Foo(){
return "Returned from function Foo";
}
When you create a function as an expression, it does not get hoisted at the top of the execution context and cannot be used before it is made. Example below:
console.log(Koo); // error
var Koo = function Koo(){
return "Return from function Koo";
}
Watch the full explanation in the video here:
Discover more from Dhananjay Kumar
Subscribe to get the latest posts sent to your email.