Wednesday, July 3, 2024
HomeLanguagesJavascriptLambda Expressions in JavaScript

Lambda Expressions in JavaScript

A lambda expression is a code you enter to define a short function. A lambda function is mostly present in modern languages (Ruby, Javascript, Java..). It’s just an expression that creates a function. 

It is very important that programming languages ​​support first-class functions, which natively pass functions as arguments to other functions or assign them to variables. This is source code text passed to the compiler and recognized using a specific syntax. (In Javascript, this is technically called an arrow function expression/declaration.) At runtime, the expression is evaluated as a lambda function in memory.

A lambda function is a short and anonymous function that takes one or more parameters and contains a single expression. Basically, you can pass a function as a parameter to another function. Because functions are treated as objects in JavaScript, they can be passed to and returned from other functions to create lambda functions.

 

Advantages of Javascript Lambda Functions:

  1. Lambda functions are pure functions in Javascript.
  2. Lambda functions are easy to read.
  3. Lambda functions are easy to cache.

Syntax:

function(arg1, arg2...argn) expression

 

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


Output:

45

In this example, the arrow function is used and we have taken two parameters and have a single expression.

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


Output:

[ 5, 6, 7, 5 ]

In this example, an anonymous function is created which makes the code small and returns the length of Names in the array.

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments