Lesson 7 of 55 · javascript
Scope, Closures & the Execution Context
Duration: 13 mins
Two main scopes:
- Global scope – accessible everywhere.
- Function scope – created by each function.
- Block scope – created by
let/const.
A closure is a function that remembers the environment in which it was created.
Example: Simple closure
function makeCounter(){
let count = 0;
return function(){
count += 1;
return count;
};
}
const counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2