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`);
-
59:14
T-SPLY
9 hours agoElected Official Busted Assaulting ICE and Local Officers!
18.9K18 -
2:16:04
Nerdrotic
6 hours ago $5.01 earnedPanspermia and Cosmic Summit with George Howard | Forbidden Frontier 103
56.8K3 -
2:32:06
vivafrei
12 hours agoEp. 265: Courts Block Trump Harvard Action! Ostriches Under Attack! Democrats Arrested & MORE!
76.3K168 -
2:21:41
IsaiahLCarter
1 day ago $8.58 earnedAPOSTATE RADIO #015: No Free Hugs (feat. Zeek Arkham)
48K1 -
LIVE
DamysusGaming
2 hours agoSplitgate 2 - Open Beta - First Play! Lets Try to Merc Some Folks and Slide on through Some Portals!
37 watching -
2:45:16
Rance's Gaming Corner
3 hours ago"She’s a Better Shot Than Me and I’m Not Okay – Splitgate 2 with My Wife"
1.95K -
4:07
megimu32
1 day agoBreathe – Faith Hill Acoustic Revival | A 2000s Nostalgia Moment
17.8K13 -
1:06:25
QNewsPatriot
1 day agoPresident Trump Confirms Q+ | George Floyd Narrative Coming Back | DEFENDER EUROPE 2025 | Patel/Bongino & Jeffrey Epstein
57.4K89 -
16:55
Clownfish TV
1 day agoThe View Told to SHUT UP by Disney Bosses!
22.7K11 -
8:54
FATTACnation
4 days ago🔴 These Targets Were NOT ready for what i Brought...
33.9K2