Skip to content

Instantly share code, notes, and snippets.

@flyptkarsh
Last active February 16, 2024 16:03
Show Gist options
  • Select an option

  • Save flyptkarsh/b076b64196cd265728c9ef0714eaf5d7 to your computer and use it in GitHub Desktop.

Select an option

Save flyptkarsh/b076b64196cd265728c9ef0714eaf5d7 to your computer and use it in GitHub Desktop.
Example of a Closure
function outerFunction() {
let outerVariable = 'I am outside!';
function innerFunction() {
// Accesses outerVariable from the outer scope
console.log(outerVariable);
}
return innerFunction;
}
// outerFunction has returned, its execution context is gone
const myClosure = outerFunction();
myClosure(); // Logs: 'I am outside!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment