Enjoy 20% off all plans by following us on social media. Check out other promotions!
Quiz Questions

Why is it, in general, a good idea to leave the global JavaScript scope of a website as-is and never touch it?

Topics
JAVASCRIPT
Edit on GitHub

TL;DR

JavaScript that is executed in the browser has access to the global scope (the window object). In general it's a good software engineering practice to not pollute the global namespace unless you are working on a feature that truly needs to be global – it is needed by the entire page. Several reasons to avoid touching the global scope:

  • Naming conflicts: Sharing the global scope across scripts can cause conflicts and bugs when new global variables or changes are introduced.
  • Cluttered global namespace: Keeping the global namespace minimal avoids making the codebase hard to manage and maintain.
  • Scope leaks: Unintentional references to global variables in closures or event handlers can cause memory leaks and performance issues.
  • Modularity and encapsulation: Good design promotes keeping variables and functions within their specific scopes, enhancing organization, reusability, and maintainability.
  • Security concerns: Global variables are accessible by all scripts, including potentially malicious ones, posing security risks, especially if sensitive data is stored there.
  • Compatibility and portability: Heavy reliance on global variables reduces code portability and integration ease with other libraries or frameworks.

Follow these best practices to avoid global scope pollution:

  • Use local variables: Declare variables within functions or blocks using var, let, or const to limit their scope.
  • Pass variables as function parameters: Maintain encapsulation by passing variables as parameters instead of accessing them globally.
  • Use immediately invoked function expressions (IIFE): Create new scopes with IIFEs to prevent adding variables to the global scope.
  • Use modules: Encapsulate code with module systems to maintain separate scopes and manageability.

What is the global scope?

In the browser, the global scope is the top-level context where variables, functions, and objects are accessible from anywhere in the code. The global scope is represented by the window object. Any variables or functions declared outside of any function or block (that is not within any module) are added to the window object and can be accessed globally.

For example:

// Assuming this is run in the global scope and not within a module.
const globalVariable = 'I am global';
function globalFunction() {
console.log('I am a global function');
}
console.log(window.globalVariable); // 'I am global'
window.globalFunction(); // 'I am a global function'

In this example, globalVariable and globalFunction are added to the window object and can be accessed from anywhere in the global context.

Pitfalls of global scope

In general, it's a good software engineering practice to not pollute the global namespace unless you are working on a feature that truly needs to be global – it is needed by the entire page. There are many reasons to avoid touching the global scope:

  • Naming conflicts: The global scope is shared across all scripts on a web page. If you introduce new global variables or modify existing ones, you risk causing naming conflicts with other scripts or libraries used on the same page. This can lead to unexpected behavior and difficult-to-debug issues.
  • Cluttered global namespace: The global namespace should be kept as clean and minimal as possible. Adding unnecessary global variables or functions can clutter the namespace and make it harder to manage and maintain the codebase over time.
  • Scope leaks: When working with closures or event handlers, it's easy to accidentally create unintended references to global variables, leading to memory leaks and performance issues. By avoiding global variables altogether, you can prevent these types of scope leaks.
  • Modularity and encapsulation: One of the principles of good software design is modularity and encapsulation. By keeping variables and functions within their respective scopes (e.g., module, function, or block scope), you promote better code organization, reusability, and maintainability.
  • Security concerns: Global variables can be accessed and modified by any script running on the page, including potentially malicious scripts. It is quite common for websites to load third-party scripts and in the event someone's network is compromised, it can pose security risks, especially if sensitive data is stored in global variables. However, in the first place you should not expose any sensitive data on the client.
  • Compatibility and portability: By relying heavily on global variables, your code becomes less portable and more dependent on the specific environment it was written for. This can make it harder to integrate with other libraries or frameworks, or to run the code in different environments (e.g., server-side vs browser).

Here's an example of global scope being used.

// Assuming this is run in the global scope, not within a module.
let count = 0;
function incrementCount() {
count++;
console.log(count);
}
function decrementCount() {
count--;
console.log(count);
}
incrementCount(); // Output: 1
decrementCount(); // Output: 0

In this example, count, incrementCount, and decrementCount are defined on the global scope. Any script on the page can access and modify the count, as well as all variables on window.

Avoiding global scope pollution

By now we hope that you're convinced that it's not a good idea to define variables on the global scope. To avoid polluting the global scope, it is recommended to follow best practices such as:

  • Use local variables: Declare variables within functions or blocks to limit their scope and prevent them from being accessed globally. Use var, let, or const to declare variables within a specific scope, ensuring they are not accidentally made global.
  • Pass variables as function parameters:: Instead of accessing variables directly from the outer scope, pass them as parameters to functions to maintain encapsulation and avoid global scope pollution.
  • Use modules: Utilize module systems to encapsulate your code and prevent global scope pollution. Each module has its own scope, making it easier to manage and maintain your code.
  • Use immediately invoked function expressions (IIFE): If modules are not available, wrap your code in an IIFE to create a new scope, preventing variables from being added to the global scope unless you explicitly expose them.
// Assuming this is run in the global scope, not within a module.
(function () {
let count = 0;
window.incrementCount = function () {
count++;
console.log(count);
};
window.decrementCount = function () {
count--;
console.log(count);
};
})();
incrementCount(); // Output: 1
decrementCount(); // Output: 0

In this example, count is not accessible in the global scope. It can only be accessed and modified by the incrementCount and decrementCount functions. These functions are exposed to the global scope by attaching them to the window object, but they still have access to the count variable in their parent scope. This provides a way to encapsulate the underlying data and only expose the necessary operations – no direct manipulation of the value is allowed.


Further reading

Edit on GitHub