Arrow Functions
Arrow Functions
π¨βπΌ Arrow functions are a concise syntax for writing functions. They're
especially useful for callbacks and short functions.
// Function declaration
function double(n: number): number {
return n * 2
}
// Arrow function equivalent
const double = (n: number): number => {
return n * 2
}
// Arrow function with implicit return (no braces!)
const double = (n: number): number => n * 2
When to Use Each Form
| Context | Use | Example |
|---|---|---|
| Top-level named functions | Function declaration | function processOrder() { ... } |
| Callbacks | Arrow function | applyToNumber(5, (n) => n + 1) |
| Short single-expression funcs | Arrow (implicit) | const double = (n) => n * 2 |
| Object methods | Method shorthand | { calculate() { ... } } |
π¨ Open
and:
- Convert the function declarations to arrow functions
- Use implicit returns where the function body is a single expression
- Create a function that uses a callback with arrow functions (no arrays)
π° Implicit returns use a single expression and no braces.
π° Callbacks are often written as arrow functions.
A callback is a function you pass to another function to be called later.
In the example above,
(n) => n + 1 is a callbackβwe're passing it to
applyToNumber, which calls it with the value 5. Functions that accept
other functions as arguments are called higher-order functions. You'll use
this pattern a lot with array methods like map, filter, and reduce.π Function Forms


