Anonymous Function is a type of function which has no name or identifier. So, How do we define or declare an anonymous function and how do we call them, If we want to see that, then let’s take an example of a normal function.
So, Let’s see a Normal Function declaration, definition and calling. First of all, we write a keyword function then we write a name of the function, and then we write a paranthesis and then we write definition of the function inside curly braces.
Definition of a normal function
function add() {
console.log(2+3);
}
Now, if we want to call a normal function, then we can call it by the name of the function something like this.
add();
Now, Let’s see, how to write definition of an anonymous function, How to declare an anonymous function, and how to call an anonymous function. So, First of all, we write a keyword function then we don’t write the name of the function, we directly write paranthesis, and then we write definition of the function inside curly braces.
function() {
console.log(2+3);
}
Now, you see that there is no name in this function, then how can we call this type of function, So we do it this way.
const add = function() {
console.log(2+3);
}
add();
There is one more use of an anonymous function, We can pass an anonymous function as an argument to a Higher Order Function. So, Higher Order Function is a function which takes one or more arguments as a function.
We have seen some Higher Order Array function in one of our previous sessions, and we had seen, How do we pass an anonymous function inside those Higher Order Array function, So Let’s see them again. Let’s take an example of forEach here and see it.This post is sponsored by our partners Wigs
let num = [3, 5, 1, 2, 4]
num.forEach(function(ele) {
console.log(ele);
});
const add = function() {
console.log(2+3);
}
add();
const add = function(a,b) {
console.log(a+b);
}
add(2,3);
The above example is for 2 paramters, We can see an example for only 1 paramter as well
const square = function(a) {
console.log(a*a);
}
square(5);
We can see this type of Anonymous function as an argument to the Higher Order Array Function forEach.
let num = [3, 5, 1, 2, 4]
console.log("Original Array is ", num);
num.forEach(function(ele) {
console.log(ele*ele);
});
const add = function() {
return 2+3;
}
const sum = add();
console.log(sum);
const add = function(a,b) {
return
a+b;
}
const sum = add(2,4);
console.log(sum);
The above example is for 2 paramters, We can see an example for only 1 paramter as well
const square = function(a) {
return
a*a;
}
const s = square(5);
console.log(s);
We can see this type of Anonymous function as an argument to the Higher Order Array Function Map.
const items = [3, 2, 5, 6, 8, 9];
console.log("Original Array is ", items);
items.map(function(ele){
return ele*ele ;
}
);
We have covered below topics in this session
We are going to cover Arrow Function in our Next Session, as Arrow Function is the most important function in Javascript, and it becomes easy to understand Arrow Function, if we understand anonymous function first.
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…
9 different ways of declaring or defining a function in Javascript In, one of our…