---
title: "What is a closure in JavaScript? Provide an example."  
description: "What is a closure in JavaScript? Provide an example."  
author: "Ravi Vishwakarma"  
published: 2024-06-17  
updated: 2024-06-23  
canonical: https://www.mindstick.com/interview/33908/what-is-a-closure-in-javascript-provide-an-example  
category: "javascript"  
tags: ["web development", "javascript", "front-end development"]  
reading_time: 3 minutes  

---

# What is a closure in JavaScript? Provide an example.

A closure in JavaScript is a feature where an inner function has access to its outer (enclosing) function's variables even after the outer function has finished executing. Closures are created whenever a function is created, allowing the function to remember the scope in which it was created.

#### Key Characteristics of Closures:

1. **Access to Outer Function’s Variables**: The inner function can access variables and parameters of its outer function even after the outer function has returned.
2. **Private Data**: Closures can be used to create private variables or methods.

## Example of a Closure:

```javascript
function outerFunction() {
    let outerVariable = 'I am an outer variable';

    function innerFunction() {
        console.log(outerVariable); // Can access the outer variable
    }

    return innerFunction;
}

const closure = outerFunction(); // outerFunction returns innerFunction, which has a reference to outerVariable
closure(); // Logs: 'I am an outer variable'
```

In this example:

- `outerFunction` declares a variable `outerVariable` and defines an `innerFunction`.
- `innerFunction` has access to `outerVariable` because it is within the scope of `outerFunction`.
- When `outerFunction` is called, it returns `innerFunction`. The `innerFunction` maintains access to `outerVariable` even after `outerFunction` has completed execution.
- Calling `closure()` (which is the returned `innerFunction`) logs the value of `outerVariable`.

## Practical Example :

```javascript
<script type="text/javascript">
	function createCounter() {
	    let count = 0; // Private variable

	    return {
	        increment: function() {
	            count++;
	            return count;
	        },
	        decrement: function() {
	            count--;
	            return count;
	        },
	        getCount: function() {
	            return count;
	        }
	    };
	}

	const counter = createCounter();

	console.log(counter.increment());
	console.log(counter.increment());
	console.log(counter.decrement());
	console.log(counter.getCount());
	console.log(counter.count);

</script>
```

## Output:

![What is a closure in JavaScript? Provide an example.](https://www.mindstick.com/interviewquestion/3354fb1f-5146-4bba-99e2-71f68f21e48e/images/0e9cde98-2887-40b2-aad0-09be45847aad.png)

## Answers

### Answer by Ravi Vishwakarma

A closure in JavaScript is a feature where an inner function has access to its outer (enclosing) function's variables even after the outer function has finished executing. Closures are created whenever a function is created, allowing the function to remember the scope in which it was created.

#### Key Characteristics of Closures:

1. **Access to Outer Function’s Variables**: The inner function can access variables and parameters of its outer function even after the outer function has returned.
2. **Private Data**: Closures can be used to create private variables or methods.

## Example of a Closure:

```javascript
function outerFunction() {
    let outerVariable = 'I am an outer variable';

    function innerFunction() {
        console.log(outerVariable); // Can access the outer variable
    }

    return innerFunction;
}

const closure = outerFunction(); // outerFunction returns innerFunction, which has a reference to outerVariable
closure(); // Logs: 'I am an outer variable'
```

In this example:

- `outerFunction` declares a variable `outerVariable` and defines an `innerFunction`.
- `innerFunction` has access to `outerVariable` because it is within the scope of `outerFunction`.
- When `outerFunction` is called, it returns `innerFunction`. The `innerFunction` maintains access to `outerVariable` even after `outerFunction` has completed execution.
- Calling `closure()` (which is the returned `innerFunction`) logs the value of `outerVariable`.

## Practical Example :

```javascript
<script type="text/javascript">
	function createCounter() {
	    let count = 0; // Private variable

	    return {
	        increment: function() {
	            count++;
	            return count;
	        },
	        decrement: function() {
	            count--;
	            return count;
	        },
	        getCount: function() {
	            return count;
	        }
	    };
	}

	const counter = createCounter();

	console.log(counter.increment());
	console.log(counter.increment());
	console.log(counter.decrement());
	console.log(counter.getCount());
	console.log(counter.count);

</script>
```

## Output:

![What is a closure in JavaScript? Provide an example.](https://www.mindstick.com/interviewquestion/3354fb1f-5146-4bba-99e2-71f68f21e48e/images/0e9cde98-2887-40b2-aad0-09be45847aad.png)


---

Original Source: https://www.mindstick.com/interview/33908/what-is-a-closure-in-javascript-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
