Last Updated : 07 Jun, 2025
A lambda expression also known as arrow-function is a function without a name defined using the arrow => syntax introduced in ES6 (ECMAScript 2015). Unlike traditional function declarations or expressions, lambda expressions are shorter and come with some unique behavior, particularly around the this keyword.
Syntax
const functionName = (parameters) => { // function body };
Now let's understand with the help of an example
JavaScript
let mul = (a, b) => a * b;
console.log(mul(5, 9));
Examples of Arrow Functions
Below are the some examples of arrow functions:
1. Basic Arrow FunctionThis is a typical arrow function that performs an operation (adding two numbers)
JavaScript
const add = (a, b) => {
return a + b;
};
console.log(add(5, 3));
2. Concise Arrow Function with Implicit Return
If the function contains only one expression, you can omit the curly braces and return statement. The result will be returned automatically
JavaScript
const mul = (a, b) => a * b;
console.log(mul(4, 5));
3. Arrow Function with No Parameters
If the function has no parameters, simply use empty parentheses:
JavaScript
const greet = () => "Hello, World!";
console.log(greet());
4. Arrow Function with One Parameter
If there is only one parameter, you can omit the parentheses:
JavaScript
const square = x => x * x;
console.log(square(6));
Use Cases for Arrow Functions 1. Using Arrow Functions with map()
Arrow functions are commonly used with array methods like map(), which require a callback function.
JavaScript
const num = [1, 2, 3, 4, 5];
const doubled = num.map(num => num * 2);
console.log(doubled);
Output
[2, 4, 6, 8, 10]2. Event Handlers
Arrow functions are often used in event listeners because they automatically bind this to the surrounding context.
JavaScript
const button = document.querySelector("button");
button.addEventListener("click", () => {
console.log(this);
});
3. Inline Functions
Arrow functions are often used for small, inline functions, where defining a function with a function keyword might seem excessive.
JavaScript
setTimeout(() => {
console.log("Hello after 1 second!");
}, 1000);
When to Use Lambda Expressions
.map()
, .filter()
, .reduce()
.this
: Especially in event handlers, timers, or nested functions.Here are some limitations:
Traditional Functions
Lambda Functions
Uses the function keyword.
Uses the => (fat arrow) syntax for a more concise function
The value of this is dynamically bound based on how the function is invoked.
this is lexically bound and takes the value from the surrounding context.
Has its own arguments object which contains all arguments passed to the function.
Does not have its own arguments object, instead inherits it from the surrounding function.
Can be used as a constructor with the new keyword to create instances.
Cannot be used as a constructor, will throw an error when used with new.
Functions have the prototype property and can be used to create methods on objects.
Arrow functions do not have a prototype property.
Suitable for functions where you need to work with this, constructors, or the arguments object.
Ideal for short functions, callbacks, and cases where this needs to be inherited from the surrounding context.
Example => function add(a, b) { return a + b; }
Example => const add = (a, b) => a + b;
ConclusionLambda expressions in JavaScript, realized via arrow functions, offer a modern, compact, and efficient way to write functions. They are particularly powerful when working with functional programming techniques or when you want to maintain the this
context without binding manually.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4