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
Sarah Westall
2 hours agoGame Changing Peptide Increases Immunity 50 to 100x Better than Nutrition Alone w/ Dr. Diane Kazer
398 watching -
2:13:56
Barry Cunningham
4 hours agoBREAKING NEWS: PRESIDENT TRUMP GIVES SPEECH AT PITTSBURGH RALLY!
19.5K16 -
LIVE
Dr Disrespect
9 hours ago🔴LIVE - DR DISRESPECT - WARZONE - SNIPING AND WINNING
2,874 watching -
1:04:55
BonginoReport
5 hours agoObama Named In Diddy Trial As Celebs Lawyer Up - Nightly Scroll w/ Hayley Caronia (Ep.59)
83.1K51 -
1:42:24
Mally_Mouse
3 hours agoLet's Play!! -- Cuphead - I'm gonna crash out..
14.4K -
LFA TV
22 hours agoLFA TV ALL DAY STREAM - FRIDAY 5/30/25
199K21 -
1:00:43
The Mike Schwartz Show
9 hours agoTHE MIKE SCHWARTZ SHOW Evening Edition 05-30-2025
10.5K3 -
41:07
JustPearlyThings
3 hours agoModern Women Have No Idea How Much Better Men Make Their Lives | Pearl Daily
24.6K20 -
LIVE
SpartakusLIVE
3 hours agoNEW Easter Egg, SPECIALIST || Duos w/ @GloryJean followed by quads later!
81 watching -
14:23
T-SPLY
6 hours agoNashville Mayor Is Getting Closer To Being Arrested For ICE Obstructions
15.8K13