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`);
-
28:54
Producer Michael
14 hours agoANSWERING YOUR MOST IMPORTANT QUESTIONS!
7.63K2 -
8:24
Hollywood Exposed
13 hours agoCNN Host Turns on Democrats LIVE And Bill Maher Can’t Believe It!
3.34K9 -
30:59
Uncommon Sense In Current Times
17 hours ago $0.45 earnedImmigration, Crime & Justice Reform | Brett Tolman on Borders, Cartels & the First Step Act
10.4K1 -
15:36
The Gun Collective
13 hours agoWOW! Tons of NEW GUNS just dropped!
4.23K10 -
LIVE
BEK TV
23 hours agoTrent Loos in the Morning - 10/08/2025
150 watching -
2:21
NAG Daily
12 hours agoPOLITICAL FAMILY GAMESHOW w/ GreenMan Reports
4.07K -
0:49
OfficialJadenWilliams
16 hours agoGTA 6 is downloading but you hear trumpets outside
4.29K4 -
1:02:11
DeVory Darkins
15 hours ago $41.70 earnedDemocrats suffers ANNIHILATION during heated hearing with Bondi as Jack Smith bombshell drops
171K133 -
57:17
Dialogue works
2 days ago $3.36 earnedAmb. Chas Freeman: Israel on the EDGE: Is TOTAL DEFEAT IMMINENT?!
39.4K21 -
14:06
Actual Justice Warrior
1 day agoSomali Medicaid SCAMMER Says Theft Is HIS CULTURE
20.4K26