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`);
-
UPCOMING
Slightly Offensive
42 minutes agoALMOST SERIOUS: How Mass Legal Immigration DESTROYED Australia | Guest: Maria Zeee
189 -
UPCOMING
The StoneZONE with Roger Stone
11 minutes agoRoger Stone Provides Crucial Update On Classified JFK Assassination Docs | The StoneZONE
-
DVR
LFA TV
1 day agoTrump vs. Karl-Theodor zu Guttenberg | TRUMPET DAILY 2.18.25 7PM
3.06K3 -
2:02:21
Revenge of the Cis
5 hours agoEpisode 1449: Boo This
41.8K8 -
1:20:31
Awaken With JP
6 hours agoWiping Hard Drives, Hire a Criminal Defense Lawyer - LIES Ep 79
83K54 -
1:02:23
In The Litter Box w/ Jewels & Catturd
23 hours agoDEI Kills | In the Litter Box w/ Jewels & Catturd – Ep. 744 – 2/18/2025
115K54 -
1:06:37
Game On!
4 hours ago $0.58 earnedBreaking Down COLLEGE BASKETBALL BETTING LINES Like a Pro!
29.4K3 -
1:01:29
John Crump Live
7 hours ago $2.41 earnedMexico Backs Cartels By Threatening To Designate Gun Manufactures As Terrorist!
30.4K9 -
2:14:09
The Nunn Report - w/ Dan Nunn
4 hours ago[Ep 611] DOGE On The March! | Guest Sam Anthony - [your]NEWS | Seditious Dems | Ukraine
32.6K11 -
1:00:56
The Tom Renz Show
11 hours agoThe War On DOGE Is ALSO A War On The Economy
44.9K8