InterviewHubPRO
Section Overview
Q219

Question 219 of 521

Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

42%
JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)

What is an anonymous function

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
  function (optionalParameters) {
    //do something
  }

  const myFunction = function(){ //Anonymous function assigned to a variable
    //do something
  };

  [1, 2, 3].map(function(element){ //Anonymous function used as a callback function
    //do something
  });

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

javascript
  var x = function (a, b) {
    return a * b;
  };
  var z = x(5, 10);
  console.log(z); // 50