Understanding JavaScript Scopes & Closures

Understanding JavaScript Scopes & Closures

Let's begin by exploring the concept of scopes: what exactly are scopes?

Scope is the current context of execution in which values and expressions which are accessible or can be referenced are available for use.?

JavaScript has four types of scopes:

1. Block Scope

Block scope, created with curly braces {}, is specifically associated with variables declared using "let" and "const". Variables declared inside blocks with "let" and "const" are accessible only inside the block where they are declared and cannot be accessed from outside the block. On the other hand, variables declared with "var", functions declared inside blocks or any other declarations may have different accessibility depending on the block of code execution and the behavior of hoisting in the JavaScript engine.

2. Function Scope

Every function in JavaScript has its own scope meaning anything declared inside function is not accessible outside the function. Everything declared inside a function has local scope.

Example :

Explanation:

  • Block Scoped Variables And Function: Inside the if (true) block, toppings and sauce are block-scoped variables.
  • Function Scoped Variables And Function: Inside the if (true) block, crustType and preparePizza is a function scoped variable and function for orderPizza. Inside the else block apologizeForNoPizza is a function scoped variable and function for orderPizza.
  • Function Scoped Variables And Function are hoisted at top and during compilation phase and while runtime initialization takes place.
  • if (true) block is executed during runtime preparePizza and crustType are initialized and is accessible throughout function orderPizza while else block is not executed therefore apologizeForNoPizza is not being initialized and inaccessible throughout function orderPizza.
  • Toppings and sauce are block scope and as per block scope they cannot be accessed outside block.
  • When we access any orderPizza function scope declarations outside orderPizza, it will throw error because as per function scope any declaration is accessible only inside the function where it is declared.

3. Global Scope

Variables which are accessible from anywhere in application are global scope.

When there is no module system in place it is easier to create global variables which are easily accessible in any file of that application.

Consider an HTML file that imports two JavaScript files: file1.js and file2.js:

In browsers, the window object represents the global scope. In other JavaScript environments, the global scope might be represented by their respective global objects. Any variable or function declared directly in the global scope becomes a property of the global object. In the case of the browser, this global object is the window object. Therefore, you can access global variables and functions using window.variableName or window.functionName in the browser.

Example :

It is advisable to use global variables only when it needs to be only accessed globally and avoid overwriting global variables because it can cause unintended side effects.

4. Module Scope

If you create a variable within a JavaScript module but outside of a function or block, it doesn't have global scope, but rather module scope. To make a module-scoped variable available to other files, you must export it from the module where it's created and then import it from the module that needs to access the variable.

Example :

Exploring the depths of JavaScript's scope landscape, we've traversed through all four scopes. Now, let's unlock the captivating world of closures.

Closures

A closure is like a special package: it includes a function and all the variables it needs from the area around it. So, when you have a function inside another function, the inner one can still use the variables from the outer one. In JavaScript, every time you create a function, you also create a closure.

Now, think of closures like having three layers:

1. The innermost layer is the function's own set of variables, called the local scope.

2. Around that, there's the scope of the function that contains it. This can be either a block of code, another function, or even the whole module.

3. Lastly, there's the outermost layer, which is the global scope, containing variables accessible throughout your entire code.

Example :

From the above cricket player example, we can see how closures in Javascript mirrors the principles of object-oriented programming (OOP), particularly in terms of encapsulation and data hiding. Before the introduction of classes in JavaScript, developers relied on closures to achieve encapsulation and emulate private methods.

A common mistake in JavaScript programming is overlooking the fact that a nested function can access not only its immediate outer function's variables but also variables from any surrounding functions. This creates a chain of function scopes that the nested function can tap into, which may lead to unexpected behavior if not properly understood and managed.

Example :

In the above example, each nested function has access to variables from its enclosing scopes. Let's break down the chain of scopes:

  • Local Scope (d): This is the innermost function's scope, where the parameter d is defined.
  • Outer Function Scope (a, b, c, e): The outer function has access to its parameters a, b, and c, as well as the global variable e.
  • Outermost Function Scope (sum function): The sum function's scope is the outermost enclosing scope. It doesn't have any local variables, but it has access to the parameters passed to it a, and since it's defined in the global scope, it also has access to the global variable e.
  • Global Scope (e): This is the outermost scope, containing the global variable e.

So, when the innermost function is executed, it can access variables a, b, c, and e from its enclosing scopes, all the way up to the global scope. This demonstrates how JavaScript's scoping allows functions to access variables defined in their outer scopes, forming a chain of scopes.

I hope you found this exploration of scopes and closures insightful and empowering! By understanding these fundamental concepts and their practical applications, you're better equipped to write robust and efficient JavaScript code. Whether you're a beginner or an experienced developer, mastering scopes and closures opens up a world of possibilities in your coding journey.

Happy learning and building!


要查看或添加评论,请登录

社区洞察

其他会员也浏览了