🫓Lexical Scope and Arrow Functions

In JavaScript, the term "lexical scope" refers to the way that the code in a function can access variables and other declarations that were in place when the function was defined. This means that variables and declarations that are defined in an outer scope (such as the global scope) are accessible to the code inside a function, but the code inside the function cannot access variables and declarations that are defined inside the function from an outer scope.

An "arrow function" is a type of function that is defined using the => syntax. Arrow functions are similar to regular functions, but they have a few differences in terms of how they behave with respect to lexical scope.

One of the main differences between regular functions and arrow functions is that arrow functions do not have their own this value. Instead, they inherit the this value from the enclosing scope. This can make it easier to work with this inside an arrow function, because you don't have to worry about the this value being changed by calling the function in different ways.

Here is an example of an arrow function that uses the this value from the enclosing scope:

Last updated