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);
console.log(result);
The output will be 25
, which is the square of 5
.
[…] https://www.devopsconsulting.in/blog/anonymous-functions-in-js/ […]
[…] Anonymous functions in js […]