Lambda Functions Vs Anonymous Functions in JavaScript.

Lambda Functions Vs Anonymous Functions in JavaScript.

Image for post

Last week, I got into a discussion with a few of my colleagues about the differences between a lambda function and an anonymous function in JavaScript. A lot of my colleagues were of the opinion that they were exactly the same. I knew they were not the same but I was getting conflicted because of the experience possessed by the developers that I was discussing with. After some discourse and research, we came to the conclusion that ?not all lambda functions are anonymous functions and vice versa?. This led me to think about the fact that a lot of developers and tech enthusiast may not know the difference, especially JavaScript developers. I will attempt to differentiate them.

An anonymous function is, as its name implies, a function without a name (no pun intended). They are most popularly used to define function expressions. An example of such usage is shown below.

//printName(“Tom”); //ReferenceError: printName is not definedconst printName = function (name){ console.log(name);}printName(“Tom”); //result: Tom

These function expressions are created at runtime and must be declared/defined before they can be called i.e. they are not hoisted, unlike function declarations that are hoisted as soon as program execution begins and can be called before its actual declaration.

Anonymous functions can also be used to promote encapsulation in Immediately Invoked Function Expressions (IIFE). This is illustrated below.

const printName = (function () { function printNameInner(name) { console.log(name); } return printNameInner;})();printName(“Tom”); //result: Tom//or in one fell swoop(function (name) { console.log(name);})(“Tom”) //result: Tom

On the other hand, lambda expressions are abstractions which enable a function to be passed around like data. In JavaScript, everything can be treated as an object, this means that a function can be sent into another function as a parameter and can also be retrieved from the called function as a return value. An example of lambda expression is as shown below.

function traverseArray(arr, func) { let result = ”; for (const value of arr) { result += func(value) + ‘ ‘; } console.log(result);}const arr = [1, 2, 3, 4, 5];const doubler = value => value * 2;////alternatively, can be written verbosely as//const doubler = (value) => {// return value * 2;//}traverseArray(arr, doubler); //result: 2 4 6 8 10

Lambda expressions have also been facilitated by the fact that most imperative programming languages support the fat arrow notation(=>) for its declaration (although Java programming language uses the thin arrow (->) symbol).

In C#, lambda expressions are usually written inline, where the function being called expects a delegate. They have been popularized by LINQ. An example of such usage is in the code snippet below.

var values = new System.Collections.Generic.List<int>() { 1, 2, 3, 4, 5 };Func<int, bool> filter = value => value > 2; //lambda expression var newValues = values.Where(filter); foreach (int newValue in newValues) Console.Write(newValue + ” “); //result: 3 4 5

The most salient point which differentiates lambda functions from anonymous functions in JavaScript is that lambda functions can be named. This naming could be of help during debugging. An example which illustrates this is shown below.

function traverseArray(arr, func) { let result = ”; for (const value of arr) { result += func(value) + ‘ ‘; } console.log(result);}const arr = [1, 2, 3, 4, 5];traverseArray(arr, function doubler(value) { return value * 2;}); //result: 2 4 6 8 10

This shows that a lambda function is not necessarily anonymous and an anonymous function is not necessarily a lambda expression if it is not passed around like data. The fat (or thin) arrow notation is simply syntactic sugar.

Most of the time, these two constructs are referred to interchangeably, and in some programming languages, finding the difference is like splitting hairs. The most important aspect of programming is grokking the underlying principles surrounding a construct and leveraging it to achieve your goals. Understanding the literature and applying it correctly improves code quality.

If you have any information to add or correct, I will be glad to hear from you. As Robert A. Heinlein said, ?I never learned from a man who agreed with me.?

No Responses

Write a response