Lambda Expressions in JavaScript
Last Updated :
19 Nov, 2024
A lambda expression is a concise way to define a short function in programming. It’s commonly found in modern languages like Python, Ruby, JavaScript, and Java. Essentially, it’s just a small piece of code that creates a function.
Thanks to functions being treated as objects in JavaScript, they can be passed to and returned from other functions, allowing the creation of lambda functions.
Example 1: In this example, the arrow function is used for showing lambda expression.
JavaScript
let multiply = (a, b) => a * b;
console.log(multiply(5, 9));
Example 2: In this example, an anonymous function is used which shows the lambda expression.
JavaScript
const Names = [
'Mansi',
'Gaurav',
'Akansha',
'Sanya'
];
console.log(Names.map(Names => Names.length));
Advantages of Lambda Functions:
- Concise Syntax
- Useful in methods like reduce and map. We do not have write a function and think of a name, we can simply provide a lambda expression.
- Automatically bind this to the surrounding context, eliminating common issues when dealing with callbacks