-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
A closure is created when a function retains access to its lexical scope, even after the outer function has finished executing.
In simple terms:
A closure is a function that remembers the variables from its outer function, even after that outer function has returned.
function outer() {
let count = 0;
return function () {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
✅ The inner function "remembers" the count
variable even after outer()
has finished.
function greet(msg) {
return function (name) {
console.log(`${msg}, ${name}`);
};
}
const sayHi = greet("Hi");
sayHi("Ravi"); // Hi, Ravi
sayHi("Neha"); // Hi, Neha
✅ Each call to greet()
creates a new closure with its own msg
.
Q1. Write a function createCounter()
that returns another function which increases and returns a private counter each time it's called.
Q2. Create a function greet(message)
that returns a function which accepts a name
and logs message + name
.
Q3. Create a function multiplier(factor)
that returns a function to multiply any number by that factor
.
Q4. Write a closure that stores a secret message and provides a method to read it but not modify it.
Q5. Create a once()
function that allows the given function to run only once and ignores subsequent calls.
Q6. Write a closure that maintains a list and provides addItem()
and getItems()
methods.
Q7. Create a closure that tracks the number of times a button is clicked (simulate clicks using a function).
Q8. Build a function makeAdder(x)
that returns another function which adds x
to its argument.
Q9. Create a timer()
closure that stores the start time and returns the elapsed time when called again.
Q10. Write a closure-based cache()
function that stores and returns computed values based on input (e.g., square of number).
Q1: What is a closure in JavaScript?
Q2: Which variables does a closure have access to?
Q3: What is the purpose of using closures?
Q4: Which concept allows a function to “remember” the environment it was created in?
Q5: In a closure, what happens to variables declared in the outer function after it returns?
Q6: Which of these is a real-life use case of closures?
Q7: Closures are most useful for: