JavaScript question detail
What are lambda expressions or arrow functions
Arrow functions (also known as "lambda expressions") provide a concise syntax for writing function expressions in JavaScript. Introduced in ES6, arrow functions are often shorter and more readable, especially for simple operations or callbacks.
Key Features:
- Arrow functions do not have their own
this,arguments,super, ornew.targetbindings. They inherit these from their surrounding (lexical) context. - They are best suited for non-method functions, such as callbacks or simple computations.
- Arrow functions cannot be used as constructors and do not have a
prototypeproperty. - They also cannot be used with
new,yield, or as generator functions.
Syntax Examples:
const arrowFunc1 = (a, b) => a + b; // Multiple parameters, returns a + b
const arrowFunc2 = a => a * 10; // Single parameter (parentheses optional), returns a * 10
const arrowFunc3 = () => {}; // No parameters, returns undefined
const arrowFunc4 = (a, b) => {
// Multiple statements require curly braces and explicit return
const sum = a + b;
return sum * 2;
};