HOWTO · JavaScript
JavaScript Lambda Function
Learn how JavaScript arrow functions work, how they differ from regular functions, and when to use each form.
On this page
In JavaScript, “lambda function” is a common search term for an arrow function. It is not a separate JavaScript construct. Arrow functions provide concise function-expression syntax, but they also behave differently from regular functions, especially for this, arguments, and construction.
What Is a Lambda Function in JavaScript?
An arrow function is a function expression written with =>. Like other functions, it can be stored in a variable, passed to another function, or returned from one. For a single expression, the expression body returns its value implicitly. A block body uses braces and needs an explicit return when it should produce a value.
Arrow Function Syntax and Examples
Use parentheses around multiple parameters; for one parameter, parentheses may be omitted. For example, const add = (a, b) => a + b has an expression body, while const add = (a, b) => { return a + b; } has a block body. Braces without return do not produce the expression’s value.
The following JavaScript program calculates a sum and maps a short list:
const add = (a, b) => a + b;
const numbers = [1, 2, 3];
const doubled = numbers.map(number => number * 2);
console.log(`Sum: ${add(2, 3)}`);
console.log(`Doubled: ${doubled.join(", ")}`);
The supplied runtime evidence confirms this output:
Sum: 5
Doubled: 2, 4, 6
How Arrow Functions Differ from Regular Functions
An arrow function takes this from its surrounding lexical scope. It does not create its own this, so it can be useful inside a callback that needs the surrounding method’s instance. A regular function instead gets this from how it is called (or from new when constructed).
Arrows also have no own arguments binding. They do not automatically read a surrounding function’s arguments; lexical lookup may find one only if an enclosing scope provides it. Use rest parameters such as (...items) => items.length when an arrow needs to collect its arguments.
An arrow function cannot be called with new, and it has no own prototype property for constructor use. A regular function declaration or expression can serve as a constructor when written to support that role. An arrow’s variable binding may have an inferred name, but the arrow itself has no named-function syntax; a named function expression is a separate form.
When to Use Arrow Functions
Arrow functions work well for short callbacks where lexical this is desired. For example, items.map(item => item.price) transforms each item into its price and returns a new array. They can also suit event callbacks when the callback should retain the surrounding this.
Choose a regular function when the function needs its own call-dependent this, must be used as a constructor, or benefits from a function declaration. For browser event listeners, remember that an arrow callback’s this is not set to the event target; use the event object when you need the target, or a regular callback when its call-bound this is appropriate.
Best Practices and Common Pitfalls
Keep an arrow body short enough to understand at a glance. For several steps, use a block body with a clear return, or choose a named function declaration when it makes the operation easier to identify. A descriptive variable name helps readers follow a function value, but it does not turn an arrow into a named function expression.
Do not choose arrow syntax solely because it is shorter. Its lexical this is useful in some callbacks and unsuitable when the function needs its own this. Arrow functions may contain side effects; whether to avoid them depends on the surrounding design, not on the syntax itself.
FAQ
What does “lambda function” mean in JavaScript?
It commonly refers to an arrow function. JavaScript’s syntax for these functions is =>; “lambda function” is a search term, not a separate language construct.
How does an arrow function handle this?
It uses this from the surrounding lexical scope and does not receive a new this based on how it is called.
Can an arrow function be a constructor?
No. Arrow functions cannot be called with new. Use a suitable regular function or class when constructing instances.
What are the benefits and limitations of arrow functions?
Their concise syntax can make callbacks easy to scan, and lexical this can avoid rebinding the surrounding context. They are not suitable when a function needs its own this, an own arguments binding, or constructor behavior. Use rest parameters when collecting arrow-function arguments.