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`);
-
LIVE
Wendy Bell Radio
5 hours agoYou Realize We Voted For This.
7,228 watching -
26:00
Neil McCoy-Ward
3 hours agoAnd So It Begins… ('Carbon' Food Quotas Are HERE)
3.21K2 -
1:09:24
Chad Prather
16 hours agoWe Must Obey God: The Gospel That Cannot Be Silenced
66.1K20 -
LIVE
The Mike Schwartz Show
1 hour agoTHE MIKE SCHWARTZ SHOW with DR. MICHAEL J SCHWARTZ 11-25-2025
3,242 watching -
LIVE
LFA TV
12 hours agoLIVE & BREAKING NEWS! | TUESDAY 11/25/25
3,023 watching -
4:50
Captain Peach
6 days ago $7.18 earned5 Ways Game Trailers Lie
52.6K2 -
18:06
We Got Receipts
19 hours agoLatest EBT Meltdowns That Are Actually Funny!
27.4K26 -
42:00
Based Campwith Simone and Malcolm
4 days agoNYT Brands Divorce as the Cool New Trend for Gen Z Girls
31.5K7 -
11:43
VSOGunChannel
21 hours ago $3.96 earnedThe Gun Control the Trump Admin is Fighting For
23.7K17 -
1:03:30
A Cigar Hustlers Podcast Every Day
1 day agoEpisode 416 Epstein Files w/Matt Booth
31.3K