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`);
-
1:01:55
VINCE
3 hours agoIs This Why They Were Hiding The Evidence? | Episode 192 - 12/19/25 VINCE
170K111 -
LIVE
Badlands Media
7 hours agoBadlands Daily: 12/19/25
3,370 watching -
14:45
Bearing
3 hours agoFROGAN Gets BODIED 💥 Caleb Hammer, Asmongold + the $20k Rejection 💰
10.7K11 -
1:10:23
Graham Allen
3 hours agoTensions ERUPT At AMFEST, Is Our Party Too Far Gone?
118K538 -
LIVE
Caleb Hammer
2 hours agoThis Has Never Happened Before | Financial Audit
269 watching -
LIVE
The Big Migâ„¢
3 hours agoTrump Drops Bombshell: Weed Reclassified to Schedule III
2,687 watching -
23:17
The Car Edition Ltd
8 hours ago $0.69 earnedFord Fiesta 1.4 Titanium Full Overhaul | Suspension, Brakes, Tyres & Engine Repair - The Car Edition
12.4K -
LIVE
Wendy Bell Radio
7 hours agoHouston, We Have A Problem
5,898 watching -
1:06:37
Chad Prather
18 hours agoHearing God, Confronting Deception, and Walking in Spirit-Filled Authority
90.8K22 -
2:20:42
Game On!
20 hours ago $5.90 earnedNFL Week 16 BEST BETS Revealed NOW!
56K7