Q6: What is a Closure in JavaScript?

A closure is the combination of a function bundled together with references to its surrounding lexical environment. Closures give inner functions access to an outer function's scope variables even after the outer function has finished executing.

Key Takeaways
  • Functions retain references to variables in their lexical environment.
  • Powers state encapsulation, module patterns, and memoization.
Knowledge Check

What will console.log print: for(var i=0; i<3; i++) { setTimeout(() => console.log(i), 100); }?