Arrow Functions & Higher-Order Functions
Write compact arrows, pass functions as values, and compose readable data pipelines.
By the end, you’ll be able to…
- Convert short function expressions into arrow functions with implicit or block returns.
- Explain why arrow functions do not create their own this binding.
- Use callbacks, array methods, and composition to build readable pipelines.
01A shorter function syntax
An arrow function is a compact function expression. It uses a parameter list, the => token, and either a single expression or a block. When the body is a single expression, the value is returned implicitly and no return keyword is needed. Parentheses around one parameter are optional; zero or multiple parameters need them.
Use a block body when the function needs several statements or an explicit return. Arrow functions are still functions: they can be stored in variables, passed as arguments, and returned from other functions. Choose the form that makes the intent easiest to read rather than shortening every function for its own sake. When an expression body returns an object literal, wrap the braces in parentheses so the parser does not mistake them for a block.
02Arrow functions and this
Arrow functions do not create their own this value. They capture this from the surrounding scope, which is useful inside a method when a nested callback needs the method's receiver. A regular function creates its own this based on how it is called.
That difference means an arrow is usually a poor choice for an object method that needs this. Use a concise method or a regular function for the method itself, and use an arrow for a callback inside it. If the method is later extracted and called on its own, the this connection disappears, so pass the object explicitly or bind the method. This is a preview; later object lessons examine this in more detail.
03Functions can be passed and returned
A higher-order function either accepts a function as an argument, returns a function, or both. The function you pass in is a callback. Callbacks let you separate a general process from the specific step that changes, such as repeating an operation or deciding which items to keep. The higher-order function owns the loop, while the callback owns the decision.
Pass named functions when the behavior is reusable and inline arrows when the behavior is short and local. Read the call as a sentence: applyTwice(3, double) means apply double twice. The clearer the names, the less the reader has to reconstruct.
04Compose readable pipelines
Array methods such as map, filter, and reduce are higher-order functions. map transforms every item, filter keeps items that pass a test, and reduce folds a list into one value. Each returns a new value; map and filter do not change the original array.
Chain small steps to form a pipeline: filter the data you want, map it into the shape you need, then reduce it to a total. Read the chain from top to bottom and keep each callback to one job. Name callback parameters after the data, not after its type, and avoid nesting a second pipeline inside the first. If a chain becomes hard to follow, assign names to the intermediate arrays or move a step into a named function.
The ideas worth keeping.
- Arrow functions shorten small callbacks but do not create their own this.
- Higher-order functions accept or return functions, which makes behavior reusable.
- Small map, filter, and reduce steps compose into pipelines that are easy to read and test.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Exercise 1: A reusable discount function
Write makeDiscount(rate) that returns an arrow function taking a price and returning the discounted price. Use it with map to discount [40, 25, 80] by 25 percent, then print the new array and the original array.
Explore a worked solution
function makeDiscount(rate) { return price => price * (1 - rate);}const prices = [40, 25, 80];const quarterOff = makeDiscount(0.25);const salePrices = prices.map(quarterOff);console.log(salePrices);console.log(prices);makeDiscount is a higher-order function because it returns a function, and the returned arrow closes over rate. map creates a new array, so the original prices remain unchanged. The output is [ 30, 18.75, 60 ] followed by [ 40, 25, 80 ].
Exercise 2: Compose two transformations
Write compose(first, second) that returns a function applying first and then second. Use it to trim and lowercase a string, then replace spaces with hyphens. Test it on ' Hello World ' and print the result.
Explore a worked solution
function compose(first, second) { return value => second(first(value));}const normalize = value => value.trim().toLowerCase();const slug = value => value.replaceAll(' ', '-');const makeSlug = compose(normalize, slug);console.log(makeSlug(' Hello World '));compose returns a new arrow that calls both steps in order. normalize runs first, removing the outer spaces and lowering the letters; slug then replaces the remaining space. The output is hello-world.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Exercise 1: A reusable discount function
Write makeDiscount(rate) that returns an arrow function taking a price and returning the discounted price. Use it with map to discount [40, 25, 80] by 25 percent, then print the new array and the original array.
[30,18.75,60] [40,25,80]
Keep exploring.
The official references behind this lesson, when you want a second angle or the full details.
One lesson closer.
Reading is a start. Trying it yourself makes the connection.