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:41:18
Glenn Greenwald
5 hours agoTerror Attacks Exploited To Push Unrelated Narratives; Facing Imminent Firing Squad, Liz Cheney Awarded Presidential Medal | SYSTEM UPDATE #381
65.2K80 -
DVR
Man in America
7 hours ago🔴 LIVE: Terror Attacks or False Flags? IT DOESN'T ADD UP!!!
11.1K1 -
1:02:38
Donald Trump Jr.
9 hours agoNew Year’s Terror, Latest Breaking News with Sebastian Gorka | TRIGGERED Ep.204
142K295 -
59:59
The StoneZONE with Roger Stone
4 hours agoAfter Years of Targeting Trump, FBI and DOJ are Unprepared to Stop Terror Attacks | The StoneZONE
27.5K7 -
1:26:42
Leonardaisfunny
3 hours ago $1.09 earnedH-1b Visas: Infinity Indians
15.6K17 -
1:08:33
Josh Pate's College Football Show
8 hours ago $0.40 earnedPlayoff Reaction Special: Ohio State Owns Oregon | Texas Survives | UGA vs Notre Dame Takeaways
18.8K3 -
58:04
Kimberly Guilfoyle
7 hours agoFBI's Terror Response Failures, Live with Steve Friend & Kyle Seraphin | Ep. 185
95.3K40 -
2:15:01
WeAreChange
8 hours agoMassive Developments In Vegas Investigation! UNREAL DETONATION, Shocking Details Emerge!
102K37 -
54:02
LFA TV
15 hours ago2025 Is Off to a Violent Start | TRUMPET DAILY 1.2.25 7pm
39.9K8 -
59:27
theDaily302
14 hours agoThe Daily 302- JJ Carrell
35.9K5