In JavaScript, you can also create anonymous functions using the function keyword. The syntax for creating an anonymous function in JavaScript is as follows:

var functionName = function(arguments) {
  // Function body

Here, functionName is a variable that holds the anonymous function. The arguments refer to the input parameters of the function, and the function body contains the code that is executed when the function is called.

Here’s an example of an anonymous function in JavaScript that calculates the square of a number:

var square = function(x) {
  return x * x;

In this example, the anonymous function takes a single argument x and returns the square of that number. You can then call the square function like any other function:

var result = square(5);

The output will be 25, which is the square of 5.

By Abhishek Singh

Software Engineer at Cotocus pvt. ltd

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x