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`);
-
19:19
Actual Justice Warrior
8 hours agoAna Kasparian DESTROYS Los Angeles Democrats
27.5K10 -
44:12
Ohio State Football and Recruiting at Buckeye Huddle
6 hours agoIs Order Finally Being Restored to College Football?
14.3K -
9:11
Faith Frontline
6 hours agoWatch Alan Ritchson EXPOSE Hollywood’s Atheism With Jesus
21.7K14 -
LIVE
VOPUSARADIO
1 day agoPOLITI-SHOCK! "DESTROY THE NARRATIVE & EXPOSE THE FRAUDS" W/ Special Guest Timothy Shea
351 watching -
1:24:25
Glenn Greenwald
7 hours agoGame of Thrones Actor Liam Cunningham on Gaza Activism and UK Censorship; Journalist Zaid Jilani on Mamdani, Epstein, the State of the Dems, and More | SYSTEM UPDATE #484
129K40 -
2:48:13
Nikko Ortiz
7 hours agoGas Station Shooting Gone Wrong
61.7K9 -
1:45:00
RiftTV
6 hours agoBETRAYAL: Bondi Persecutes HERO DOCTOR Under Trump DOJ | The Rift | Marc Lobliner & Lilly Gaddis
40.7K15 -
7:29:03
RalliedLIVE
11 hours ago $3.87 earnedSHOTTY BOYS vs TRIPLE THREAT THURSDAY
69.9K2 -
1:21:59
Donald Trump Jr.
7 hours agoButler: The Untold Story, Interview with Salena Zito | TRIGGERED Ep.257
163K131 -
1:00:56
BonginoReport
8 hours agoBen Shapiro Wants You To Move On From Epstein - Nightly Scroll w/ Hayley Caronia (Ep.87)
91.9K115