Lesson 10
Closures
Understand function scope deeply
1What is a Closure?✨
A closure is a powerful feature in JavaScript that allows a function to remember the environment in which it was created. This means the function can access variables from its outer scope even after that outer function has finished executing!
2Why Learn Closures?🎯
- ✓ Enables powerful patterns in JavaScript
- ✓ Helps manage private variables
- ✓ Useful for callback functions and event handling
- ✓ Essential for functional programming techniques
3Understanding Scope💡
In JavaScript, scope determines where variables can be accessed. There are two main types of scope: global and local. Variables defined inside a function are not accessible outside of it.
4Example of Scope:🚀
function outerFunction() {
let outerVar = "I am outside!"; function innerFunction() {
console.log(outerVar);
} innerFunction();
}outerFunction(); // Outputs: I am outside!
5Creating a Closure🔍
When an inner function is defined within an outer function, it forms a closure. The inner function retains access to the variables of the outer function even after the outer function has completed.
6Closure Example:⭐
function makeCounter() {
let count = 0; return function() {
count++;
return count;
};
}const counter = makeCounter();
console.log(counter()); // Outputs: 1
console.log(counter()); // Outputs: 27Practical Uses of Closures🧩
Closures can be used to create private variables, manage state, and implement data hiding. They are frequently used in event handling and asynchronous programming.
8Example of Private Variables:🎈
function secret() {
let secretCode = "12345"; return function() {
return secretCode;
};
}const getSecret = secret();
console.log(getSecret()); // Outputs: 123459Wrap-Up✨
Closures are an essential part of JavaScript that allow for more flexible and powerful coding techniques. Understanding closures will deepen your knowledge of function scope and help you write cleaner, more efficient code!
🎯 Practice Challenges
Master Closures by completing all 3 difficulty levels
Closures - Easy Challenge
Start with the basics. Perfect for beginners!
Closures - Medium Challenge
Get more challenging. Build your confidence!
Closures - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Closures? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 9: JSON & Data FormatsParse and build structured data
- Next lesson →Lesson 11: ES6 ModulesSplit code across import/export files
- Grade 10 courseC Programming - System ProgrammingC coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 9 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.