Introduction
JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.
Before moving further let us look at the previous articles of the series:
- Voice of a Developer: JavaScript Data Types - Part One
- Voice of a Developer: JavaScript Objects - Part Two
- Voice of a Developer: JavaScript Engines - Part Three
- Voice of a Developer: JavaScript Common Mistakes - Part Four
- Voice of a Developer: Editors - Part Five
- Voice of a Developer: VSCode - Part Six
- Voice of a Developer: Debugging Capabilities of VSCode - Part Seven
- Voice of a Developer: JavaScript OOP - Part Eight
- Voice of a Developer: JavaScript Useful Reserved Keywords - Part Nine
- Voice of a Developer: JavaScript Functions - Part Ten
- Voice of a Developer: JavaScript Functions Invocations - Part Eleven
In the last article I covered functions in depth. Now, I will cover Anonymous Function in detail covering its advantages, disadvantages & differences.
We know what a function is and we need to understand what anonymous function is. Anonymous means,
A function without a name is an anonymous function. We store these inside a variable name. So, the invocation happens using the variable name.
Below is an example
Anonymous Function
- var sum = function(a,b){return a+b;};
- sum();
Advantages of an anonymous function
The first advantage is that JavaScript allows you to pass an anonymous functions as an object to other functions. You don’t need to explicitly define the function before passing it as a parameter to another function. Example: below is a distance calculator between two points. In method calculateDistanceMetres we’re passing an anonymous function.
- function calculateDistanceMetres(pointA, pointB, method) {
- var value = method();
- return(pointB - pointA) * value;
- }
- console.log(calculateDistanceMetres(10, 20, function() {
- return 1000;
- }));
Now, what if we don’t pass anonymous function then what will happen?
- console.log(calculateDistanceMetres(10, 20));

In order to solve the above case, let us adjust our code which will cover both scenarios

Rajib Das GuptaPosted May 19, 2016, 11:58 AM
Very Nice article :)
Debasis SahaPosted May 3, 2016, 9:30 AM
Good One..
Vignesh ManiPosted May 3, 2016, 8:38 AM
Nice
Bhuvanesh MohankumarPosted May 3, 2016, 8:13 AM
Good one
Amit DavePosted May 3, 2016, 6:51 AM
Good share!