implicitGlobals
Prevents implicit global variable declarations in browser scripts.
✅ This rule is included in the browser logical preset.
In browser scripts (non-module code), top-level var declarations and function declarations create properties on the global window object.
This can lead to unexpected behavior and naming conflicts with other scripts loaded in the same page.
This rule flags top-level var declarations and function declarations in non-module scripts.
It encourages developers to either use modules (with import/export), use let or const declarations (which don’t create globals), or explicitly assign to window if global access is truly needed.
Examples
Section titled “Examples”var globalCounter = 0;
function incrementCounter() { globalCounter++;}// Use let or const - they don't create window propertieslet globalCounter = 0;
function incrementCounter() { globalCounter++;}// Or use a module with exportexport const globalCounter = 0;
export function incrementCounter() { // ...}// Or explicitly assign to window if truly neededwindow.globalCounter = 0;
window.incrementCounter = function () { window.globalCounter++;};Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you’re working with legacy non-module browser code where globals are intentional and there’s no risk of naming conflicts, you might disable this rule. For modern applications, prefer using modules and avoiding implicit globals.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
no-implicit-globals