1

Can someone please explain the difference between lexical scope and closure? I'm taking the Advanced JavaScript course on PluralSight. The way the instructor explains lexical scope and then closures confuses me.

From my understanding, lexical scope defines how variable names are resolved within nested functions... but isn't this the same as closures since lexical scoping is also involved in closures? Maybe my understanding of closures is incorrect?

MariposaM
  • 107
  • 1
  • 12
  • 2
    Lexical Scope simply means that the scope of a variable is based on where it is declared (the environment it is contained in). A closure is really just a use-case of that. In other words, closures exist in JavaScript because JavaScript is lexically scoped. A very loose analogy might be "heart attacks are a possibility in beings that have hearts". Just because you have a heart doesn't mean you'll have a heart attack, but because you do, you might. ;) – Scott Marcus Dec 08 '17 at 21:21
  • Yes, JS closures use lexical scoping. – Bergi Dec 08 '17 at 23:24

0 Answers0