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`);
-
LIVE
Matt Kohrs
10 hours agoTesla Stock Is SQUEEZING (Market Open Analysis) || Live Trading
922 watching -
LIVE
Randi Hipper
12 minutes agoBIG WEEK FOR BITCOIN! LATEST PRICE UPDATE
83 watching -
LIVE
LFA TV
11 hours agoLFA TV ALL DAY STREAM - MONDAY 9/15/25
4,989 watching -
1:26:16
JULIE GREEN MINISTRIES
3 hours agoLIVE WITH JULIE
75.8K168 -
LIVE
The Pete Santilli Show
2 hours agoOur Memorial & Prayer Vigil For Charlie Kirk - September 15, 2025
502 watching -
LIVE
Welcome to the Rebellion Podcast
22 hours ago $1.03 earnedWhere Do We Go From Here? - WTTR Podcast Live 9/15
303 watching -
LIVE
The Chris Salcedo Show
17 hours ago $2.77 earnedFALLOUT: Charlie Kirk's Assassination
614 watching -
LIVE
The Bubba Army
2 days agoCharlie Kirk's Shooter, Had a Trans BF? - Bubba the Love Sponge® Show | 9/15/25
1,434 watching -
30:31
DeVory Darkins
16 hours ago $8.12 earnedCNN forced to admit the shocking truth about shooter as Fetterman drops bomb on Democrats
27.2K84 -
20:54
Professor Nez
17 hours ago🚨BREAKING: Shooter’s Own Words in Charlie Kirk Case Revealed
13.5K14