JS Coding Questions Logo
JS Coding Questions
#215💼 Interview💻 Code

What is an anonymous function

Advertisement

728x90

An anonymous function is a function without a name! Anonymous functions are commonly assigned to a variable name or used as a callback function. The syntax would be as below,

javascript
1function (optionalParameters) {
2  //do something
3  }
4
5const myFunction = function(){ //Anonymous function assigned to a variable
6  //do something
7};
8
9[1, 2, 3].map(function(element){ //Anonymous function used as a callback function
10  //do something
11});

Let's see the above anonymous function in an example,

javascript
1var x = function (a, b) {
2  return a * b;
3  };
4
5var z = x(5, 10);
6
7console.log(z); // 50

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 45

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
215of476