Premium Only Content
Learn JavaScript CLOSURES in 10 minutes! 🔒
// closure = A function defined inside of another function,
// the inner function has access to the variables
// and scope of the outer function.
// Allow for private variables and state maintenance
// Used frequently in JS frameworks: React, Vue, Angular
00:00:00 intro
00:00:31 example 1
00:02:19 example 2
00:07:00 example 3
00:10:08 conclusion
// ---------- EXAMPLE 1 ----------
function outer(){
const message = "Hello";
function inner(){
console.log(message);
}
inner();
}
message = "Goodbye";
outer();
// ---------- EXAMPLE 2 ----------
function createCounter() {
let count = 0;
function increment() {
count++;
console.log(`Count increased to ${count}`);
}
function getCount() {
return count;
}
return {increment, getCount};
}
const counter = createCounter();
counter.increment();
counter.increment();
counter.increment();
console.log(`Current count: ${counter.getCount()}`);
// ---------- EXAMPLE 3 ----------
function createGame(){
let score = 0;
function increaseScore(points){
score += points;
console.log(`+${points}pts`);
}
function decreaseScore(points){
score -= points;
console.log(`-${points}pts`);
}
function getScore(){
return score;
}
return {increaseScore, decreaseScore, getScore};
}
const game = createGame();
game.increaseScore(5);
game.increaseScore(6);
game.decreaseScore(3);
console.log(`The final score is ${game.getScore()}pts`);
-
7:51
Dr. Nick Zyrowski
6 days agoHow To Starve Fat Cells - Not Yourself!
36.4K6 -
1:11:53
DeVory Darkins
2 hours agoBREAKING: Hegseth drops NIGHTMARE NEWS For Mark Kelly with potential court martial
91.6K41 -
LIVE
Dr Disrespect
4 hours ago🔴LIVE - DR DISRESPECT - ARC RAIDERS - BLUEPRINTS OR DEATH
2,738 watching -
1:10:26
Sean Unpaved
3 hours agoJalen Hurts & Eagles COLLAPSE In LOSS vs. Cowboys | UNPAVED
16.5K2 -
2:00:25
Steven Crowder
5 hours agoNo Influencer Safe: New X Update Exposed A Major Psyop
422K254 -
17:38
Professor Nez
3 hours agoTHIS will HAUNT Jasmine Crockett for Years...
14.9K20 -
56:36
The Rubin Report
4 hours agoTense Moment at Trump-Mamdani Meeting That No One Predicted
46.7K62 -
9:34
The White House
4 hours agoFirst Lady Melania Trump Welcomes the Official 2025 White House Christmas Tree
35.2K16 -
LIVE
LFA TV
17 hours agoLIVE & BREAKING NEWS! | MONDAY 11/24/25
1,944 watching -
1:01:32
VINCE
6 hours agoNew Info Blows the Lid Off The Butler Assassination Attempt | Episode 175 - 11/24/25 VINCE
267K241