no-shadow-restricted-names

Disallow variables named undefined, NaN, Infinity, eval, arguments or globalThis

warn by default

undefined, NaN and Infinity look like keywords, but they're ordinary globals, so a variable or a parameter can take their name. After const undefined = "none", every value === undefined in that scope compares with "none".

The rule reports variables, parameters, functions, classes and imports with one of these names.

This rule is on by default.

Configure#

On by default as a warning. To turn it off:

bunfig.toml
[lint.rules]
"no-shadow-restricted-names" = "off" # or "error"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-shadow-restricted-names" = ["warn", {
  reportGlobalThis = true,
}]

Examples#

example.js2 problems
const undefined = "none";

function toNumber(text, NaN) {
  return text === "" ? NaN : Number(text);
}
example.jsNo problems
const missing = "none";

function toNumber(text, fallback) {
  return text === "" ? fallback : Number(text);
}

A variable named undefined that never gets a value is allowed, because it's the same as the global:

example.jsNo problems
var undefined;

Options#

OptionDefaultWhat it does
reportGlobalThistrueAlso report globalThis.

If your project has an ESLint config and ESLint 9 or older installed, the default is false, as it is there.

example.js1 problem
const globalThis = window;
example.jswith { "reportGlobalThis": false }No problems
const globalThis = window;