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
Major League Fishing
4 days agoLIVE! - Fishing Clash Team Series: Heritage Cup - Day 1
2,010 watching -
18:40
itsSeanDaniel
4 hours agoPiers Morgan CALLED OUT and HUMILIATED by Andrew Tate
24.2K177 -
LIVE
Times Now World
2 days agoLIVE: "ON CAM: How the Charlie Kirk Shooter ESCAPED – Shocking New Footage Revealed!"
379 watching -
1:45:51
Game On!
23 hours ago $10.77 earnedWise Guys Reveal NFL Week 2 BEST BETS Now
64.7K8 -
26:57
Robbi On The Record
2 days agoMouth Breathing Is Why You’re Exhausted | with Dr. Melanie Silvestrini
28.3K5 -
LIVE
Total Horse Channel
7 hours ago2025 Reno Snaffle Bit Futurity | Sunday Finals
114 watching -
40:44
SouthernbelleReacts
7 days ago $3.49 earned“Event Horizon (1997) Reaction | Hellraiser in Space with Sam Neill & Laurence Fishburne”
37K4 -
10:49
Artur Stone Garage
3 days ago $2.50 earnedI Spent $2000 on My Turbo Honda Civic Build (Before & After)
37.7K10 -
0:44
Danny Rayes
19 hours ago $3.89 earnedDid Someone Know It Was Going To Happen?
37.3K11 -
15:03
World2Briggs
1 day ago $3.18 earnedShocking Home Prices in Florida's Cheapest Towns!
32K7