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
.
I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains:
-
DevOps School – Tech blogs and tutorials
-
Holiday Landmark – Travel stories and guides
-
Stocks Mantra – Stock market strategies and tips
-
My Medic Plus – Health and fitness guidance
-
TrueReviewNow – Honest product reviews
-
Wizbrand – SEO and digital tools for businesses
I’m also exploring the fascinating world of Quantum Computing.
[…] https://www.devopsconsulting.in/blog/anonymous-functions-in-js/ […]
[…] Anonymous functions in js […]