DEV Community

Gift Egbonyi
Gift Egbonyi

Posted on

Understanding JavaScript Closures

Hey friends! πŸ‘‹

I couldn't make it yesterday and I feel sad about that πŸ˜”. I lost a relative so I was at her wake. But I'm here today :)

Today is closures, one of those concepts that seems a bit complex but actually very practical once you see it in action.

Closures occur when a function remembers variables from its outer scope, even after that outer function has finished running.


Example 1: Basic Closure

function greet(name) {
  const greeting = 'Hello';
  return function() {
    console.log(`${greeting}, ${name}!`);
  }
}

const sayHello = greet('Alice');
sayHello(); // "Hello, Alice!"
Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

  • greet('Alice') returns a function.
  • That returned function remembers the variables greeting and name.
  • Even though greet has finished executing, sayHello() still works.

Example 2: Counter with Closure

Closures are perfect for keeping private variables.

function createCounter() {
  let count = 0;
  return function() {
    count++;
    return count;
  }
}

const counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3
Enter fullscreen mode Exit fullscreen mode
  • count is private. You can’t access it directly from outside.
  • Each call remembers the previous value.

Example 3: Real-World Use Case

function makeMultiplier(x) {
  return function(y) {
    return x * y;
  }
}

const double = makeMultiplier(2);
const triple = makeMultiplier(3);

console.log(double(5)); // 10
console.log(triple(5)); // 15
Enter fullscreen mode Exit fullscreen mode

Closures let us create flexible and reusable functions.


Here is a Challenge for You

No codepen today but can you answer the question below?

function secretPassword(password) {
  return function(guess) {
    return guess === password ? 'Access granted' : 'Access denied';
  }
}

const checkPassword = secretPassword('1234');
console.log(checkPassword('0000')); // ?
console.log(checkPassword('1234')); // ?
Enter fullscreen mode Exit fullscreen mode

β€œWhy does this work even though secretPassword has finished running?”


πŸ™‹πŸΎβ€β™€οΈ Wrap-Up

  1. A closure is a function remembering its scope even after execution.
  2. Useful for private data, partial application, and factory functions.
  3. Always think: β€œWhich variables does this function remember?”

That’s it for today!
What should we talk about next Wednesday? Drop it below πŸ‘‡

Connect with me on GitHub

Was this tutorial helpful? Got questions? Drop them in the πŸ’¬, I love feedback.


I’m keeping these light, fun, and useful, one small project at a time.
If you enjoyed this, leave a πŸ’¬ or 🧑 to let me know.

Follow me for more short, beginner-friendly JavaScript lessons every week.

I'm still hunting for full stack roles,
contract or full-time.
Please check out my [Portfolio](https://gift-egbonyi.onrender.com)
Enter fullscreen mode Exit fullscreen mode

Portfolio

Web trails:
LinkedIn | X (Twitter)

See you next Wednesday πŸš€, on time!

Till then, write clean code and stay curious. πŸ¦‹

Top comments (0)