Home JavaScript - Closure and Callback Function
Post
Cancel

JavaScript - Closure and Callback Function

Yes, cb(user)** in your code is both a Closure and a Callback Function**. Let me explain how it holds both these properties.


Great question! Closures and callback functions are related concepts in JavaScript, but they are not the same thing. Let me break it down for you so the difference (and the connection) is crystal clear.


1. What is a Closure?

A closure is a function that “remembers” the variables from its outer scope even after that scope has exited. In simpler terms, a closure allows a function to access variables that were defined outside of its immediate context.

Example of a Closure:

1
2
3
4
5
6
7
8
9
10
11
12
13
function outerFunction() {
  let outerVariable = "I am from the outer scope";

  function innerFunction() {
    console.log(outerVariable); // innerFunction remembers outerVariable
  }

  return innerFunction;
}

const closureFunction = outerFunction(); // outerFunction is called
closureFunction(); // Output: "I am from the outer scope"

  • The innerFunction is a closure because it captures and remembers the variable outerVariable from the outer function’s scope, even after outerFunction has finished executing.

2. What is a Callback Function?

A callback function is a function that is passed as an argument to another function and is executed later, often in response to some event or after an asynchronous operation.

Example of a Callback Function:

1
2
3
4
5
6
7
8
9
10
11
function fetchData(callback) {
  setTimeout(() => {
    console.log("Data fetched");
    callback("Data"); // Call the provided callback function
  }, 1000);
}

fetchData((data) => {
  console.log("Callback executed with:", data);
});

  • Here, the function (data) => { console.log("Callback executed with:", data); } is a callback function because it is passed to fetchData and executed later when the setTimeout finishes.

Comparison: Closures vs. Callback Functions

AspectClosureCallback Function
DefinitionA function that “remembers” variables from its outer scope.A function passed as an argument to another function.
PurposeTo preserve state or access variables after the outer function has executed.To define a task to execute later, often asynchronously.
TimingUsed at any time when a nested function needs access to outer variables.Typically executed after a specific event or operation completes.
Example Use CasePreserving state for incremental counters or private variables.Handling data after fetching it from an API.

A callback function can also use closures to remember variables from its outer scope. In fact, many real-world examples of callbacks involve closures.

Example Combining Closure and Callback Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function createCounter() {
  let count = 0;

  return function incrementAndCallback(callback) {
    count++;
    callback(count); // Pass the updated count to the callback
  };
}

const counter = createCounter();

counter((count) => {
  console.log("Current count:", count); // Output: Current count: 1
});

counter((count) => {
  console.log("Current count:", count); // Output: Current count: 2
});

  • Closure: The incrementAndCallback function has access to count, even though the createCounter function has already executed.
  • Callback: The (count) => { console.log("Current count:", count); } function is passed as a callback to handle the result.

Key Takeaways

  • A closure is about retaining access to variables from an outer scope.
  • A callback function is about defining behavior to execute later.
  • They overlap when a callback function uses closures to access variables from its surrounding scope.

Closures are a foundational concept in JavaScript, while callbacks are a common programming pattern. Understanding both helps you write cleaner, more efficient code. Let me know if you’d like more examples or a deeper dive into either concept! 😊

This post is licensed under CC BY 4.0 by the author.