I was pretty shocked to my lack of deeper understanding of Javascript, so I started to read You Don't Know JS and watch Deep JavaScript Foundations by Kyle Simpson on Frontendmasters again! Repetition really matters….!

Understanding Scope in Javascript

What is Scope?

  • The ability to store values and pull values out of variables is what gives a program state.
  • But, where do those variables live?, where are they stored?, and how does our program find them when it need them?
  • set of rules for storing variables : Scope

.

  • JavaScript engine performes many of the same steps as compiled languages do.
  • For JavaScript,the compilation that occurs happends mere microseconds before the code is executed. (JS engines use many tricks like JITs)

.

  • In short, any snippet of JavaScript has to be compiled before (usually right before!) it’s executed.

Understanding Scope

  • Engine: responsible for start-to-finish compilation and execution.
  • Compiler: handles parsing and code-generation.
  • Scope: collects and maintains a look-up list of all the declared identifiers.

.

how it works?

two distinct actions are taken for a variable assignment

  1. Compiler declares a variable (if not previously declared in the current scope)
  2. when executing, Engine looks up the variable in Scope and assigns to it, if found.

. .