JavaScript Lambda Function

About JavaScript Lambda

In JavaScript, a lambda function, also known as an arrow function, is a concise way to define an anonymous function.

JavaScript lambda function is used to write more concise, expressive, and functional code.

A lambda function does not require 'function' keyword and a specific name for the function.

The "fat arrow" (=>) symbol is used in lambda function.

If you want to call the function, you need to assign the function to a variable. The variable name will be used to call the function.

Lambda functions are often used as callback functions and are commonly used with array methods like map(), filter(), and reduce().

Simple Syntax Lambda Function

Simple syntax of lambda function is given below:

Breakdown of Lambda Function
  • Parameters: A lambda function starts with parentheses '()' and parameter/parameters separated by comma are placed inside the parentheses (function may not have a parameter). The (parameter1, parameter2, ...) are the function parameters.
  • Arrow (=>): The fat arrow => separates the parameters from the function body.
  • Function Expression: This part defines the code of the function to be executed.

Create Lambda Function

In the following example, a lambda function is created to add two numbers.

Example of Lambda Function
Output of the above example
27
                                
Explanation of the code
  • const add = (a, b) => a + b;: const add declares a constant variable named 'add' and the lambda function (a, b) => a + b is assigned to the variable. The function starts with two parameters a and b. The function expression a + b is the return value of the function.
  • console.log(add(12, 15));: This calls the add function by passing 12 as the value of a and 15 as the value of b.
Example of Lambda Function for map() Method
Example of Lambda Function for filter() Method
Example of Lambda Function for reduce() Method

JavaScript Compiler For Live Practice

Practice the above JavaScript code in the following JavaScript Editor. Create more JavaScript lambda/array functions in the following editor and click Execute button to check the output. The output will be displayed in JavaScript Code Output section.

JavaScript logo JavaScript Editor
JavaScript Code Output