Undeclared Variables in Non-Strict JavaScript

In non-strict JavaScript, assigning a value to a variable that was never declared with var, let, or const automatically creates a new property on the global object, turning it into an implicit global variable. This behavior bypasses standard lexical scoping rules, leading to unintended consequences such as global scope pollution, accidental data overwrites, and memory leaks.

How Implicit Global Creation Works

When the JavaScript engine encounters an assignment expression, such as x = 10;, it begins an identifier lookup by traversing the scope chain. It checks the current local scope, any outer function scopes, and finally the global scope.

If no prior declaration of the variable is found anywhere in the chain, non-strict mode assumes the variable should exist globally. The engine creates a new property with that identifier directly on the global object (window in web browsers or global in Node.js) and assigns the specified value to it.

function setCoordinates() {
  // 'latitude' is not declared with let, const, or var
  latitude = 40.7128; 
}

setCoordinates();
console.log(window.latitude); // 40.7128 (accessible globally)

Differences Between Implicit and Declared Globals

While an undeclared assignment functions like a global variable, it has distinct behavioral differences from a variable declared globally with var:

Risks of Relying on Undeclared Variables

Implicit global variables introduce several structural risks into a codebase:

Prevention with Strict Mode

To eliminate the silent creation of implicit globals, ECMAScript 5 introduced strict mode. Adding "use strict"; at the top of a JavaScript file or inside a specific function alters the assignment behavior.

In strict mode, assigning a value to an undeclared variable immediately halts execution and throws a ReferenceError: <variable> is not defined, ensuring all variables are explicitly declared before use.