An anonymous function is a function that has no name, hence can’t be called using its name. Anonymous function is assigned and associated with a variable since, in JavaScript, variables can be of type function.
An anonymous function is stored in a variable using an expression defining the function body. The variable holding a function is used as a function name
var x = function (a, b) {return a * b};
After an anonymous function is stored inside variable, a variable holding a function can be used as normal function call.
<!DOCTYPE html>
<html>
<body>
<p>This example calls a function which performs a calculation, and returns the result:</p>
<p id="demo"></p>
<script>
var multiply = function (a, b) {return a * b};
var ans = multiply(25, 7);
document.getElementById("demo").innerHTML = ans;
</script>
</body>
</html>
Any normal function can be converted to anonymous function with only minor changes.
// calculates square of given number function calculateSquare(num) { return num * num; }
var calculateSquare = function(num) {return num * num;}
Another version of anonymous function
(function() { return num * num; })
Adding a brackets () at the end of above anonymous function will make it self executing
(function() { return num * num; }) ();