Lambda Expressions and Javascript

Have you ever heard of the term “lambda”? Does it sound like a complicated thing to you? Don’t be scared! After reading this short post, it won’t be that scary.

Lambda Expressions in Various Programming Languages

So, how do they describe Lambda Expressions in different programming languages?

1-Lambda Expressions are nameless functions given as constant values. They can appear anywhere that any other constant may, but are typically written as a parameter to some other function (source).

2-A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls (source).

3-In some programming languages, anonymous functions (like java) are known as Lambda Expressions (source).

4-In C#, to have a Lambda Expression you should write an expression like “Parameters => Executed code”:

You can read n => n % 2 == 1 like: “input parameter named n goes to anonymous function which returns true if the input is odd” (source). Below is a piece of C# code with a lambda expression:

Lambda Expressions in Javascript

Okay, let’s see the Lambda Expressions in Javascript. Actually, we have a similar notation to the C# notation displayed above. See below for a couple of Javascript functions declared as lambda expressions:

Below is how you can call these functions:

As you can see, we call functions declared with lambda expressions just like we call any other Javascript function. Easy right? I think seeing a simple sample is the best way to understand this concept and I believe you pretty much know how to write a lambda expression in Javascript now.

There is also this notion of lambda expressions in Javascript. Some say using an anonymous function as an argument in a call to another function also creates a lambda expression. See below:

So, no arrows or anything. Just a function used as a parameter in a function call. Just keep that in mind also. When you are talking to people about lambda expressions in Javascript they could be talking about this second alternative.

Hope this helps.
Good Luck,

Leave a Reply

Your email address will not be published. Required fields are marked *