Arrow Functions
Prerequisites: Functions
An arrow function is a shorter way to write a function, especially for short callbacks — the most common style in modern JavaScript.
const greet = (name) => {
console.log(`Hello, ${name}!`);
};
// A single expression can skip the braces and "return":
const square = (n) => n * n;
console.log(square(6)); // 36
Arrow functions are especially common as arguments to array methods:
numbers.map((n) => n * 2).
Common mistakes
- Adding both
{ }and expecting an implicit return — with braces, you must writereturnexplicitly:(n) => { return n * n; }.