Disallow the use of undeclared variables unless mentioned in /*global */
comments
The "extends": "eslint:recommended"
property in a configuration file enables this rule
This rule can help you locate potential ReferenceErrors resulting from misspellings of variable and parameter names, or accidental implicit globals (for example, from forgetting the var
keyword in a for
loop initializer).
Any reference to an undeclared variable causes a warning, unless the variable is explicitly mentioned in a /*global ...*/
comment, or specified in the globals
key in the configuration file. A common use case for these is if you intentionally use globals that are defined elsewhere (e.g. in a script sourced from HTML).
Examples of incorrect code for this rule:
Examples of correct code for this rule with global
declaration:
Note that this rule does not disallow assignments to read-only global variables. See no-global-assign if you also want to disallow those assignments.
This rule also does not disallow redeclarations of global variables. See no-redeclare if you also want to disallow those redeclarations.
typeof
set to true will warn for variables used inside typeof check (Default false).Examples of correct code for the default { "typeof": false }
option:
You can use this option if you want to prevent typeof
check on a variable which has not been declared.
Examples of incorrect code for the { "typeof": true }
option:
Examples of correct code for the { "typeof": true }
option with global
declaration:
For convenience, ESLint provides shortcuts that pre-define global variables exposed by popular libraries and runtime environments. This rule supports these environments, as listed in Specifying Environments. A few examples are given below.
Examples of correct code for this rule with browser
environment:
Examples of correct code for this rule with node
environment:
If explicit declaration of global variables is not to your taste.
This rule provides compatibility with treatment of global variables in JSHint and JSLint.
This rule was introduced in ESLint v0.0.9.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-undef