Arrow functions
suggest changeIntroduction
Arrow functions provide concise function syntax. Unlike traditional functions, they do not define their own this, arguments, or new.target; those values are resolved from the surrounding scope. Arrow functions cannot be used as constructors.
Syntax
const double = (x) => x * 2; // Implicit return
const add = (x, y) => { return x + y; }; // Explicit return
const makePoint = (x, y) => ({ x, y }); // Parenthesize an object literal
const load = async () => fetch("/data"); // Async arrow function
(() => console.log("run now"))(); // Immediately invoked
A line terminator is not allowed between the parameter list and =>. A line may follow the arrow, although parentheses are often clearer for multiline expressions.
Use a traditional function when the function needs a dynamic this, its own arguments, or constructor behavior with new.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents