---
title: "What are closures in JavaScript, with a simple example?"  
description: "What are closures in JavaScript, with a simple example?"  
author: "Marta Kowalska"  
published: 2026-11-13  
updated: 2026-11-13  
canonical: https://www.mindstick.com/forum/163012/what-are-closures-in-javascript-with-a-simple-example  
category: "JavaScript and Frontend"  
tags: ["javascript", "closures", "scope", "functions", "lexical scope"]  
reading_time: 1 minute  

---

# What are closures in JavaScript, with a simple example?

I keep hearing the word closure in interviews. Can someone explain it simply?

## Replies

### Reply by Sagar Poudel

A closure is a function together with the variables from the place where it was created. The function keeps access to those variables even after the outer function has returned.

```
function makeCounter() {
  let count = 0;
  return function () {
    count++;
    return count;
  };
}
const next = makeCounter();
next(); // 1
next(); // 2
```

The inner function still uses `count`, although makeCounter finished long ago. Each call to makeCounter creates its own separate count.

Closures are used for private data, callbacks, event handlers, partial application and things like debounce. A common warning: closures keep variables alive, so holding on to big objects in a long-living closure can leak memory.


---

Original Source: https://www.mindstick.com/forum/163012/what-are-closures-in-javascript-with-a-simple-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
