Last active
February 16, 2024 16:03
-
-
Save flyptkarsh/b076b64196cd265728c9ef0714eaf5d7 to your computer and use it in GitHub Desktop.
Example of a Closure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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