Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is lexical scope in JavaScript

Understanding 'Lexical Scope' in JavaScript

If you're making your way through the world of programming, you might have come across the term 'lexical scope'. It might seem a bit intimidating, but it's actually a pretty simple concept once you break it down. So, let's dive in and demystify lexical scope in JavaScript.

What is Lexical Scope?

To understand what 'lexical scope' means, let's first look at the words 'lexical' and 'scope'.

'Lexical' is a fancy word that comes from linguistics, which means 'relating to words or vocabulary'. In programming, it refers to the arrangement of words in your code. On the other hand, 'scope' in programming refers to the region or section of your code where a variable or a function can be accessed.

So, 'lexical scope' means the scope (or accessibility) of a variable or a function is determined by the structure of your code, or where the variable or function is written in your code.

How Does Lexical Scope Work in JavaScript?

Before we dive into examples, it's important to note that JavaScript uses lexical scoping. This means that functions in JavaScript are lexically (or statically) scoped. The scope of a variable or function is defined by where it is declared, not where it is used or called.

Let's look at a simple example:

let outer = "I'm the outer scope!";

function showScopes() {
  let inner = "I'm the inner scope!";

  console.log(outer); // "I'm the outer scope!"
  console.log(inner); // "I'm the inner scope!"
}

showScopes();
console.log(outer); // "I'm the outer scope!"
console.log(inner); // Error: inner is not defined

In this example, outer is in the global scope, so it can be accessed anywhere in the code. However, inner is declared inside the function showScopes, so it can only be accessed within that function. This is lexical scoping in action!

Lexical Scope and Nested Functions

Things get more interesting when we have nested functions. A nested function is a function declared inside another function. In JavaScript, a nested function has access to the scope of the outer function.

Here's an example:

function outerFunction() {
  let outerVar = "I'm in the outer function!";

  function innerFunction() {
    console.log(outerVar); // "I'm in the outer function!"
  }

  innerFunction();
}

outerFunction();

In this case, innerFunction is a nested function, and it can access outerVar from the outer function. This is because innerFunction is lexically scoped within outerFunction.

The Analogy

Think of lexical scoping like a set of Russian nesting dolls. The smallest doll (the innermost function or variable) has access to the space inside all the larger dolls (outer functions or variables). But the larger dolls can't see or access what's inside the smaller dolls.

Wrapping Up

In conclusion, lexical scope is a fundamental concept in JavaScript and many other programming languages. Understanding it can help you write cleaner, more predictable code. It's like learning the rules of the road before you start driving. You could probably figure out how to drive without knowing the rules, but you'd likely end up in a lot of confusing and dangerous situations.

Remember, the scope of a variable or function in JavaScript is determined by where it's declared in the code, not where it's used. When in doubt, think back to our Russian doll analogy - the smaller dolls (inner functions or variables) can see out, but the larger dolls (outer functions or variables) can't see in.

So, keep exploring, keep coding, and you'll find that concepts like lexical scope start to become second nature. Happy coding!