Array callback
Runs once for each item in methods such as map, filter and forEach.
A callback is a function passed into another function so it can be called later or for each item.
const numbers = [1, 2, 3]; const doubled = numbers.map((value) => value * 2); console.log(doubled);
Functions & Scope
A callback is not a special syntax. It is a normal function used as an argument. The receiving function decides when to call it.
Callbacks appear in array methods, event listeners, timers, promises, Node-style APIs and custom utility functions.
The key skill is reading control flow: your callback is defined now, but it may run later, once per item or only when an event happens.
Runs once for each item in methods such as map, filter and forEach.
Runs when the browser fires an event.
Runs later after a delay or interval.
A function you design can accept behavior as an argument.
Examples
const isLarge = (value) => value >= 10; const largeValues = values.filter(isLarge);
items.map((item) => {
validate(item);
save(item);
render(item);
notify(item);
});
// This callback has too many responsibilities.
Code patterns
These examples focus on syntax you will actually look up later: declarations, callbacks, returns, closures and context handling. Use the small patterns first, then study the deeper explanation.
Transform each item into a new value.
const values = [1, 2, 3]; const doubled = values.map((value) => value * 2); console.log(doubled);
Keep only items that match a condition.
const values = [4, 9, 12, 3]; const large = values.filter((value) => value >= 10); console.log(large);
The browser calls it after an interaction.
button.addEventListener("click", () => {
output.textContent = "Clicked";
});
Design a function that accepts behavior.
function repeat(times, callback) {
for (let index = 0; index < times; index += 1) {
callback(index);
}
}
repeat(3, (index) => console.log(index));
Rules that matter
Functions are where JavaScript stops being a list of statements and becomes a system. Scope decides what each function can see, while return values and parameters decide how functions communicate.
They are normal values passed as arguments.
Immediate, later, once per item or after an event are different flows.
Extract large callbacks into named functions.
map transforms, filter selects, forEach performs side effects.
Later callbacks need error paths too.
Promises and async functions can flatten async flow.
Production thinking
Callbacks are everywhere in JavaScript. Understanding them unlocks array methods, DOM events and asynchronous behavior.
Event callbacks often update UI state, messages and focus. Keeping them clear helps preserve accessible behavior.
Production callbacks should be small, named where useful and explicit about side effects.
Callbacks that render lists or links should not hide important content behind unclear side effects.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Functions only become useful when you can explain what goes in, what comes out and which scope the function can see.