prefer-global-this

April 8, 2026 ยท View on GitHub

๐Ÿ“ Prefer globalThis over window, self, and global.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

This rule will enforce the use of globalThis over window, self, and global.

However, there are several exceptions that remain permitted:

  1. Certain window-specific APIs, such as window.innerHeight
  2. Window-specific events, such as window.addEventListener('resize')
  3. Computed property access on window, such as window[foo]

The complete list of permitted APIs can be found in the rule's source code.

Examples

// โŒ
window;

// โœ…
globalThis;
// โŒ
window.foo;

// โœ…
globalThis.foo;
// โŒ
global;

// โœ…
globalThis;
// โŒ
global.foo;

// โœ…
globalThis.foo;
// โŒ
const {foo} = window;

// โœ…
const {foo} = globalThis;
// โŒ
window.navigator;

// โœ…
globalThis.navigator;
// โŒ
window.location;

// โœ…
globalThis.location;
// โœ…
window.innerWidth;

// โœ…
window.innerHeight;
// โŒ
window.addEventListener('click', () => {});

// โœ…
globalThis.addEventListener('click', () => {});

// โœ…
window.addEventListener('resize', () => {});

// โœ…
window.addEventListener('load', () => {});

// โœ…
window.addEventListener('unload', () => {});