anonymous function :
A function that is declared without any named identifier is known as an anonymous function. In general, an anonymous function is inaccessible after its declaration.
Ex :
var anon = function() {
alert('I am anonymous');
};
anon();
Arrow Function :
Arrow functions were introduced in the ES6 version of javascript.
They provide us with a new and shorter syntax for declaring functions.
Arrow functions can only be used as a function expression.
Let’s compare the normal function declaration and the arrow function declaration in detail:
Ex:// Traditional Function Expression
var add = function(a,b){
return a + b;
}
// Arrow Function Expression
var arrowAdd = (a,b) => a + b;
Arrow functions are declared without the function keyword. If there is only one returning expression then we don’t need to use the return keyword as well in an arrow function as shown in the example above.
Also, for functions having just one line of code, curly braces { } can be omitted.
Ex:// Traditional Function Expression
var multiplyBy2 = function(num){
return num * 2;
}
// Arrow Function Expression
var arrowMultiplyBy2 = num => num * 2;
If the function takes in only one argument, then the parenthesis () around the parameter can be omitted as shown in the code above.