Skip to content

Callback

In one line

A callback is a function passed as an argument to another function, to be called later when an event or operation completes.

In simple words

A callback is a function you give to another function, saying "call this when you are done." The receiving function calls your callback at the appropriate time — after an operation completes, when data is ready, or when an event occurs.

Callbacks are everywhere in JavaScript: event handlers (call this function when the user clicks), array methods (call this function for each element), and asynchronous operations (call this function when the data arrives).

For example, arr.map(callback) calls your callback once for each element. button.addEventListener('click', callback) calls your callback when the button is clicked. The pattern is the same: you provide a function, and something else calls it at the right time.

Example

javascript
// Callback with array method
const doubled = [1, 2, 3].map(n => n * 2);
console.log(doubled); // [2, 4, 6]

// Callback with event handler
button.addEventListener('click', function() {
  console.log('Button was clicked!');
});

// Callback with asynchronous operation
setTimeout(() => {
  console.log('This runs after 1 second');
}, 1000);

In each case, a function is passed as an argument and called later by the receiving function — that is a callback.

Common confusions

  • Confused with: promise

    The difference: A callback is a function passed to be called later; a Promise is an object representing a future value. Promises were created to handle async operations more cleanly than nested callbacks (callback hell).

Related terms

Related Resources

Related Lessons

  • Functions

    Learn about functions and callbacks.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary