Mark As Completed Discussion

One Pager Cheat Sheet

  • Closures are relatively new and potentially confusing, but with practice and the help of MDN docs, we can finally understand them!
  • Understanding the Scope of Execution is essential to properly utilize closures, as a program without functions will only have one global scope.
  • The program outputs 27 because the console.log() statement references the initial declared a value before the sum function changes it.
  • A stack data structure is used to store a newly created local scope when a function is called, which is popped off the stack and destroyed when the scope of execution ends.
  • Understanding nested functions and their ability to access globalScope variables is essential for fully comprehending the concept of closures.
  • The variable declared in the global scope is available inside the inner function, but the other way around is not possible and will throw an error.
  • The ReferenceError thrown is due to innerLocalScope not being accessible from the global scope, as it is scoped to the innerScopeCheck() method.
  • The output of this code snippet is determined by the scope of the variables used in the innerScopeCheck function, with innerLocalScope being limited in scope.
  • The code snippet demonstrates the concept of closure by showing how an incremented value of a variable can be "remembered" across subsequent executions of a function.
  • A JavaScript closure combines a function with its surrounding state to create a new entity, which can be further investigated using the Inspect command in Google Chrome.
  • A JavaScript closure is a function combined with the local scope in which it was declared and is maintained after the outer function has finished executing.
  • Closures offer powerful flexibility to remember the associated environment of an innerScopeCheck() method to have access to changed values of variables and utilize it in code, as demonstrated in this scopeCheck() example.