Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 11 / 31FUNCTIONS

Functions are values.

Passing behavior lets one function shape another's work.

01 / THE BASICS

Start here.

A function definition creates a value that can be stored in a name, passed to another function, or returned. The name double refers to a function; double(3) calls it and produces a number. Omitting parentheses when passing double means 'hand over this function so something else can call it'. A callback is simply a function supplied to other code for that purpose.

Arrays offer methods that use callbacks. map visits every element and builds a new array from the callback's returned values. In prices.map(price => price * 1.1), price is the current element. The arrow function returns a number for each one, so the result is a new array of numbers. The original prices array remains as it was. filter instead keeps an original element when its callback returns a truthy value; it does not turn every element into a boolean.

Different methods use different callback agreements. map asks for a replacement value, filter asks for a keep-or-discard decision, and some asks whether any element passes. The callback may run immediately, as map's does, or much later, as an event handler does. Read the method's agreement before reading the callback body; it tells you what each parameter represents and how the returned value will be used.

02 / THE IDEA

Build the model.

A JavaScript function can be stored, passed, and returned like other values. A callback is a function handed to another function for later or repeated use. The caller that receives the callback decides when to invoke it; that timing is part of its contract.

Array methods such as map, filter, and some use callbacks to describe a transformation, selection, or test. They express intent well when each callback is small. A long callback with hidden effects can be harder to follow than a named function or an explicit loop.

03 / UNDER THE SURFACE

How it works.

A callback is passed without calling it at that moment. In prices.map(price => price * 1.1), map receives a function value, then calls it once for each array item. The price parameter takes the current item, and the callback's return value becomes the corresponding item in a new array. If you forget to return, the new array contains undefined at those positions.

Different array methods ask different questions of their callbacks. map asks 'what should replace each item?', filter asks 'should this item stay?', and some asks 'does any item pass?'. The callback may be an arrow function or a named function. Keep the choice connected to the result you want, and avoid mutating the source array inside a transformation unless that effect is deliberate.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const prices = [10, 20, 30];
const taxed = prices.map(price => price * 1.1);

First notice. map creates a new array; it does not replace prices.

map begins with 10, calls the arrow function, and stores 11 in its new array. It repeats with 20 and 30, storing 22 and 33. The original prices array still contains 10, 20, 30. The callback itself does not know about map; map is the code choosing when and how often to call it.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const scores = [42, 81, 67];
const passing = scores.filter(score => score >= 50);
console.log(passing);

The result is [81, 67]. filter calls the callback for each score and keeps an item when its callback result is truthy. It does not transform 81 into true in the output; the boolean only decides whether 81 stays.

06 / KEEP THIS

One thing to remember.

A callback is an ordinary function with a timing agreement.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.