Js equivalent to Python Lambda
Last Updated :
16 Dec, 2024
In Python, lambda functions provide a way to create small, anonymous functions. These functions are useful for short term tasks where defining a full function with a name would be hectic. JavaScript doesn't have a direct equivalent to Python's lambda keyword, but it offers arrow functions that can achieve the same functionality. In this article, we will explore how JavaScript's arrow functions serve as the equivalent of Python's lambda functions, and we will compare the two.
Python Lambda Functions
In Python, the lambda keyword is used to create small anonymous functions. These functions can have any number of arguments but only a single expression. The result of the expression is automatically returned by the lambda function.
Example of a Python Lambda Function
Python
add = lambda x, y: x + y
print(add(3, 5))
Explanation:
- In this example, lambda x, y: x + y defines a function that takes two parameters, x and y, and returns their sum. This anonymous function is assigned to the variable add, which can then be called like a regular function.
- The lambda function eliminates the need for a full function definition.
JavaScript Arrow Functions
In JavaScript, the closest equivalent to Python’s lambda function is the arrow function. Arrow functions were introduced in ES6 (ECMAScript 2015) and provide a concise syntax for defining anonymous functions. Like Python's lambda, JavaScript arrow functions are typically used for short, single-expression functions.
Syntax of JavaScript Arrow Function
const add = (x, y) => x + y;
Example of a JavaScript Arrow Function
JavaScript
const add = (x, y) => x + y;
console.log(add(3, 5));
Explanation:
- The JavaScript arrow function const add = (x, y) => x + y is an anonymous function that adds two numbers, similar to Python’s lambda function.
- The => syntax separates the parameters (x, y) from the function body x + y. Arrow functions are concise and readable, making them ideal for simple, single-expression operations.
JavaScript Equivalent to Python Lambda Functions
Below are some of the approaches by which we can show the equivalence between Python lamda functions and JS arrow functions:
Example 1: Sorting an Array
Both Python and JavaScript lambda functions (or arrow functions) are commonly used for operations like sorting arrays.
Python Sorting Example Using Lambda Function
Python
n = [5, 3, 8, 1, 2]
n.sort(key=lambda x: x)
print(n)
Explanation:
- In this example, the lambda function is used as the sorting key to sort the n list in ascending order.
- After sorting, the list [1, 2, 3, 5, 8] is printed.
JavaScript Sorting Example Using Arrow Function
JavaScript
const n = [5, 3, 8, 1, 2];
n.sort((a, b) => a - b);
console.log(n);
Explanation:
- In this example, an arrow function (a, b) => a - b is used as the comparison function to sort the n array in ascending order.
- The sorted array [1, 2, 3, 5, 8] is then printed.
Example 2: Filtering an Array
Lambda functions and arrow functions are often used with filter() to select elements from a list or array based on a condition.
Python Filtering Example Using Lambda Function
Python
n = [1, 2, 3, 4, 5, 6]
even = list(filter(lambda x: x % 2 == 0, n))
print(even)
Explanation:
- In this example, the lambda function lambda x: x % 2 == 0 is used with the filter() function to select even numbers from the n list. The result [2, 4, 6] is printed.
JavaScript Filtering Example Using Arrow Function
JavaScript
const n = [1, 2, 3, 4, 5, 6];
const even = n.filter(x => x % 2 === 0);
console.log(even);
Explanation:
- In this example, an arrow function x => x % 2 === 0 is used with the filter() method to select even numbers from the numbers array.
- The result [2, 4, 6] is then printed.
Similar Reads
Js Equivalent to Python In In Python, it is used for iterating over elements, checking membership and even used for conditional checks. JavaScript offers similar functionality with different syntax and behavior. This article discusses how Python's in keyword works in different contexts and how to achieve similar behavior in J
3 min read
JS Equivalent to Python Slicing In Python, slicing is a feature that allows us to extract portions of a sequence such as lists, strings and tuples using a simple syntax. However, JavaScript does not have a direct equivalent to Pythonâs slicing syntax. Fortunately, there are several ways to achieve similar functionality in JavaScri
4 min read
JS equivalent to Python List comprehension Pythonâs list comprehension is an efficient way to create lists by performing operations on each item of an existing iterable, such as a list or range. JavaScript doesn't have a built-in list comprehension feature but it provides several methods to achieve similar functionality. In this article, we
3 min read
Python Lambda Functions Python Lambda Functions are anonymous functions means that the function is without a name. As we already know the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python. In the example, we defined a lambda function(u
6 min read
Javascript equivalent to Python Dictionary A dictionary is a powerful data structure allowing us to store and access data using keys quickly. In Python, we use a dictionary to store key-value pairs. In JavaScript, the closest equivalent to a Python dictionary is the Object or Map. The choice between a JavaScript Object or Map depends on the
3 min read