We all know functions are group of statements that perform a specific task. Functions in JavaScript exhibit various forms .We have a variety of types in functions in JavaScript , like function expression, Anonymous functions, Arrow functions and IIFE.
Lets get started with some basic function pattern which we normally see in other programming languages as well i.e. normal/regular function definition.
Syntax:
function function_name(){
//statements
}
This was pretty easy! Now lets have a look at some other types.
Function Expressions
In JavaScript we can create functions and can assign them to variables as well!
Yes , you read it right! We can assign functions to variables.
Syntax:
var variable_name =function function_name(){
//statements
}
We cannot call sayhi() directly, it results into reference error.
Also remember , function expressions are never hoisted!
So we cannot do something like this.
This results into TypeError.
Anonymous Functions
Anonymous functions are functions with no specific name.
- They can be stored in a variable.
- Can be returned in a function .
- Can be passed in a function.
function(){
//body of function
};
Store an anonymous function in a variable.
var variable_name = function(){
//body of function;
}
Passing anonymous functions as an argument in JavaScript.
Now if we call myfunc inside show we get,
Returning anonymous function
Arrow Functions!
Arrow functions were introduced from ES6.
This was all for this blog!
Hope it helps!
Happy learning!
Top comments (0)