Javascript Tutorial – Anonymous Function in Javascript

What is Anonymous Function

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.

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();

Anonymous Function

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);
});

Types of Anonymous Functions

  1. Anonymous Functions with no parameters and no return type
  2. Anonymous Functions with parameters and no return type
  3. Anonymous Functions with no parameters but with return type
  4. Anonymous Function with parameters and with return type

Anonymous Functions with no parameters and no return type

const add = function() {
console.log(2+3);
}

add();

Anonymous Functions with parameters and no return type

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);
});

Anonymous Functions with no parameters but with return type

const add = function() {
return 2+3;
}

const sum = add();
console.log(sum);

Anonymous Function with parameters and with return type

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 ;
 } 
);

Summary

We have covered below topics in this session

  1. What is Anonymous Function
  2. Normal Function
  3. Anonymous Function
  4. Types of Anonymous Functions

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.

Leave a Comment