A closure is a function that remembers and can access variables from outside its own scope, even after the outer function has finished running.
Click to reveal answer
beginner
Why do closures remember variables from their outer function?
Because when a function is created inside another function, it keeps a reference to the outer function's variables, so it can use them later.
Click to reveal answer
intermediate
How can closures be useful in real life?
Closures help keep data private and create functions with their own memory, like counters or settings that remember past values.
Click to reveal answer
intermediate
What will the following code output and why?<br><pre>function makeCounter() {<br> let count = 0;<br> return function() {<br> count += 1;<br> return count;<br> }<br>}<br>const counter = makeCounter();<br>console.log(counter());<br>console.log(counter());</pre>
It will output: 1 2 Because the inner function remembers the variable 'count' from makeCounter and increases it each time it is called.
Click to reveal answer
advanced
Can closures cause memory issues? Why or why not?
Yes, closures can keep variables in memory longer than needed if not managed carefully, because they hold references to outer variables even after the outer function ends.
Click to reveal answer
What does a closure allow a function to do?
AChange the global variables directly
BRun faster than normal functions
CAccess variables from its outer function even after the outer function has finished
DCreate new variables in the global scope
✗ Incorrect
Closures let a function remember and access variables from the outer function's scope even after that function has returned.
In this code, what will be logged?
function outer() { let name = 'Alice'; return function() { console.log(name); } } const inner = outer(); inner();
Anull
Bundefined
CError
D'Alice'
✗ Incorrect
The inner function remembers the variable 'name' from the outer function and logs 'Alice'.
Which of these is NOT a benefit of closures?
AAutomatically freeing memory
BCreating private variables
CMaintaining state between function calls
DData privacy
✗ Incorrect
Closures do not automatically free memory; in fact, they can keep variables in memory longer.
What happens if you create multiple closures from the same outer function?
AAll closures share the same variables
BEach closure has its own separate copy of the outer variables
COnly the first closure works correctly
DClosures cannot be created multiple times
✗ Incorrect
Each closure keeps its own copy of the outer function's variables, so they do not interfere with each other.
Which keyword is used to define a function that can create a closure?
Afunction
Bvar
Clet
Dconst
✗ Incorrect
The 'function' keyword defines functions, which can create closures when nested.
Explain in your own words what a closure is and why it is useful.
Think about how a function can keep some information even after it finishes.
You got /3 concepts.
Describe a simple example where a closure helps keep track of a count or state.
Imagine a counter that remembers how many times it was called.
You got /3 concepts.
Practice
(1/5)
1. What is a closure in JavaScript?
easy
A. A function that has no parameters
B. A function that runs only once
C. A function that returns another function without variables
D. A function that remembers variables from its creation context
Solution
Step 1: Understand closure definition
A closure is a function that keeps access to variables from the place where it was created, even after that place has finished running.
Step 2: Match definition to options
A function that remembers variables from its creation context correctly describes this behavior, while others describe unrelated concepts.
Final Answer:
A function that remembers variables from its creation context -> Option D
Quick Check:
Closure = function remembering variables [OK]
Hint: Closures remember variables from where they were made [OK]
Common Mistakes:
Thinking closures are functions without parameters
Confusing closures with immediately invoked functions
Believing closures only run once
2. Which of the following is the correct syntax to create a closure in JavaScript?
easy
A. function outer() { let x = 5; function inner() { return x; } }
B. function outer() { let x = 5; return x; }
C. function outer() { let x = 5; return function inner() { return x; } }
D. function outer() { return function() { let x = 5; } }
Solution
Step 1: Identify closure syntax
A closure requires an inner function that accesses variables from the outer function and is returned so it can be used later.
Step 2: Check options for correct closure
function outer() { let x = 5; return function inner() { return x; } } returns an inner function that uses outer variable x, forming a closure. Others either don't return the function or don't access outer variables correctly.
Final Answer:
function outer() { let x = 5; return function inner() { return x; } } -> Option C
Quick Check:
Return inner function accessing outer variable [OK]
Hint: Closure needs returned inner function accessing outer variable [OK]
Common Mistakes:
Not returning the inner function
Returning only the variable, not a function
Defining inner function but not using outer variables