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`);
-
2:01:53
Inverted World Live
6 hours agoPentagon Monster Hunters and One Year Since Butler, PA | Ep. 73
19.6K4 -
9:21:38
Dr Disrespect
14 hours ago🔴LIVE - DR DISRESPECT - UNBELIEVABLE WARZONE CHALLENGES
211K15 -
1:40:10
AimControllers
7 hours ago $5.59 earned🔴LIVE - THE PRE GAME LOBBY PODCAST Ep. 2
29.5K1 -
19:19
Actual Justice Warrior
10 hours agoAna Kasparian DESTROYS Los Angeles Democrats
34.8K16 -
44:12
Ohio State Football and Recruiting at Buckeye Huddle
8 hours agoIs Order Finally Being Restored to College Football?
28.8K -
9:11
Faith Frontline
8 hours agoWatch Alan Ritchson EXPOSE Hollywood’s Atheism With Jesus
34.2K20 -
4:29:52
VOPUSARADIO
1 day agoPOLITI-SHOCK! "DESTROY THE NARRATIVE & EXPOSE THE FRAUDS" W/ Special Guest Timothy Shea
30.5K -
1:24:25
Glenn Greenwald
9 hours agoGame of Thrones Actor Liam Cunningham on Gaza Activism and UK Censorship; Journalist Zaid Jilani on Mamdani, Epstein, the State of the Dems, and More | SYSTEM UPDATE #484
141K43 -
2:48:13
Nikko Ortiz
9 hours agoGas Station Shooting Gone Wrong
68.5K9 -
1:45:00
RiftTV
8 hours agoBETRAYAL: Bondi Persecutes HERO DOCTOR Under Trump DOJ | The Rift | Marc Lobliner & Lilly Gaddis
44.2K21