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`);
-
4:14
The Rubin Report
1 day agoDave Rubin Shares Behind-the-Scenes Story of What Charlie Kirk Did for Him
77.1K59 -
1:58:58
Badlands Media
1 day agoDevolution Power Hour Ep. 389: Psyops, Patsies, and the Information War
117K178 -
2:13:55
Tundra Tactical
11 hours ago $16.45 earnedTundra Talks New Guns and Remembers Charlie Kirk On The Worlds Okayest Gun Show Tundra Nation Live
62.8K13 -
1:45:08
DDayCobra
13 hours ago $43.11 earnedDemocrats Caught LYING Again About Charlie Kirk's KILLER
97.2K101 -
19:23
DeVory Darkins
15 hours ago $21.59 earnedShocking Update Released Regarding Shooter's Roommate as Democrats Issue Insane Response
83.7K188 -
19:53
Stephen Gardner
17 hours ago🔥EXPOSED: Charlie Kirk Shooter's Trans Partner Tells FBI EVERYTHING!
85.7K370 -
2:47:25
BlackDiamondGunsandGear
12 hours agoAfter Hours Armory / RIP Charlie Kirk / What we know
64.4K8 -
29:09
Afshin Rattansi's Going Underground
2 days agoThe Political Life of Malcolm X: Busting the Myths (Prof. Kehinde Andrews)
64.1K17 -
2:47:25
DLDAfterDark
12 hours ago $8.22 earnedThe Assassination of Charlie Kirk - Just What We KNOW
42.1K9 -
1:33:56
MattMorseTV
13 hours ago $44.92 earned🔴Exposing his PARTNER IN CRIME.🔴
94.7K301