๐ Scenario: Imagine you are building a simple counter app. You want to create counters that remember their own counts even after you use them multiple times. This is like having a personal tally for each counter.
๐ฏ Goal: You will build a function that creates counters using closures. Each counter will keep its own count and increase it when called.
๐ What You'll Learn
Create a function that returns another function
Use a variable inside the outer function to store the count
The inner function should increase and return the count
Create at least one counter using the function
Show the count increasing by calling the counter multiple times
๐ก Why This Matters
๐ Real World
Closures help keep data private and create functions with memory, like counters, timers, or settings in apps.
๐ผ Career
Understanding closures is important for JavaScript developers to write clean, efficient, and bug-free code that manages state safely.
Progress0 / 4 steps
1
Create the outer function with a count variable
Write a function called createCounter that declares a variable count and sets it to 0 inside the function.
Javascript
Hint
Remember to use let to declare the count variable inside the function.
2
Return an inner function that increases count
Inside the createCounter function, write a return statement that returns a new function. This inner function should increase count by 1 and return the new value.
Javascript
Hint
The inner function should access and change the count variable from the outer function.
3
Create a counter and call it multiple times
Create a variable called myCounter and set it to the result of calling createCounter(). Then call myCounter() three times, storing each result in variables first, second, and third.
Javascript
Hint
Call createCounter() once and save it to myCounter. Then call myCounter() three times and save each result.
4
Print the results to see the counts
Use console.log to print the values of first, second, and third on separate lines.
Javascript
Hint
Use console.log three times to print each count value on its own line.
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