JavaScript Lexical Scope Explained

JavaScript doesn’t decide variable access at runtime. It decides it at write-time. That’s Lexical Scope. JavaScript looks for variables based on where the code is written, not where a function is called from. How lookup works: • Check local scope • Then parent scope • Then parent’s parent • Finally global This upward lookup is called the Scope Chain. A Lexical Environment is simply: Local memory + reference to its parent environment Important rules: • Inner functions can access outer variables • Globals are accessible (but dangerous) • Anything outside the chain = not defined JavaScript doesn’t guess. It follows the structure you wrote. If you understand this, closures, hoisting confusion, and “undefined errors” stop being mysterious. #JavaScript #LexicalScope #ScopeChain #NamasteJavaScript #LearningInPublic #FrontendDevelopment #WebDevelopment

To view or add a comment, sign in

Explore content categories