Skip to main content

Command Palette

Search for a command to run...

Demystifying Lexical Scope in JavaScript

Updated
โ€ข2 min readโ€ขView as Markdown
Demystifying Lexical Scope in JavaScript
M

hey there ๐Ÿ‘‹ i'm Samia, a Backend developer turning to frontend world. i used to work as a Laravel and PHP developer for 3 and half years, in that journey, i had to deal from time to time with some frontend tasks, like fixing responsiveness or converting some psd files into HTML & CSS, that was when i fall in love with it.

I loved the idea of turning creative and beautiful design into reality, so i made that career switch and now turning fully to Frontend development.

Feel free to checkout my journey on Instagram or just pass Hi

Scope is a fancy word for what data is available to me at any line of code

Lexical scope, in simple terms, is all about where you declare your functions. This choice determines which data a function can access when it's executed

To illustrate this concept, consider the following example:

// global scope
function outer() {
  const outerMessage = "Hello from outer!";

  function inner() {
    const innerMessage = "Hello from inner!";
    console.log(`${innerMessage} and ${outerMessage}`);
  }

  return inner;
}

const myFunc = outer();
myFunc(); // Outputs: "Hello from inner! and Hello from outer!"

In this case, innerMessage is accessible to the inner() function because it's scoped within its parent function.

Note that outerMessage is also accessible because it resides in the parent scope of inner(), here is how this is possible

  • JavaScript initially looks for a variable in the current scope (inner() in this case).

  • If the variable is not found there, it continues the search in the parent scope (outer() in this case).

  • If it's still not found, JavaScript keeps climbing the scope chain until it reaches the global scope.

  • If the variable is not found in the global scope, it returns a reference error, indicating that the variable is not defined.

This process of traversing up to the global scope is known as Scope Chaining.

Understanding Lexical Scope and Scope Chaining is crucial for writing effective JavaScript code. It empowers you to control the accessibility of variables in your functions.

Happy coding! ๐Ÿš€๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

#JavaScript #LexicalScope #ScopeChaining #WebDevelopment #Programming #CodingDemystified

More from this blog

๐”๐ง๐๐ž๐ซ๐ฌ๐ญ๐š๐ง๐๐ข๐ง๐  ๐ฉ๐ซ๐จ๐ญ๐จ๐ญ๐ฒ๐ฉ๐š๐ฅ ๐ข๐ง๐ก๐ž๐ซ๐ข๐ญ๐š๐ง๐œ๐ž

as developers we deal with objects most of the time, but have you ever wondered how JavaScript objects can access a treasure trove of built-in methods, seemingly by magic? Let's demystify it! Take this as an example: ๐’„๐’๐’๐’”๐’• ๐’–๐’”๐’†๐’“ = { ๐’๐’‚๐’Ž๏ฟฝ...

Oct 17, 20232 min read
๐”๐ง๐๐ž๐ซ๐ฌ๐ญ๐š๐ง๐๐ข๐ง๐  ๐ฉ๐ซ๐จ๐ญ๐จ๐ญ๐ฒ๐ฉ๐š๐ฅ ๐ข๐ง๐ก๐ž๐ซ๐ข๐ญ๐š๐ง๐œ๐ž

Untitled Publication

5 posts

PHP & Laravel developer switching to Frontend realm, i enjoy writing blogs and sharing my knowledge, follow my instagram to see my journey to becoming a frontend developer