Open In App

Js equivalent to Python Lambda

Last Updated : 16 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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))

Output
8

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));

Output
8

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)

Output
[1, 2, 3, 5, 8]

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);

Output
[ 1, 2, 3, 5, 8 ]

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)

Output
[2, 4, 6]

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);

Output
[ 2, 4, 6 ]

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.

Next Article
Article Tags :
Practice Tags :

Similar Reads