12 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).
16 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](../user-guide/configuring/language-options#using-configuration-files-1). A common use case for these is if you intentionally use globals that are defined elsewhere (e.g. in a script sourced from HTML).
18 Examples of **incorrect** code for this rule:
23 /*eslint no-undef: "error"*/
25 var foo = someFunction();
31 Examples of **correct** code for this rule with `global` declaration:
36 /*global someFunction, a*/
37 /*eslint no-undef: "error"*/
39 var foo = someFunction();
45 Note that this rule does not disallow assignments to read-only global variables.
46 See [no-global-assign](no-global-assign) if you also want to disallow those assignments.
48 This rule also does not disallow redeclarations of global variables.
49 See [no-redeclare](no-redeclare) if you also want to disallow those redeclarations.
53 * `typeof` set to true will warn for variables used inside typeof check (Default false).
57 Examples of **correct** code for the default `{ "typeof": false }` option:
62 /*eslint no-undef: "error"*/
64 if (typeof UndefinedIdentifier === "undefined") {
71 You can use this option if you want to prevent `typeof` check on a variable which has not been declared.
73 Examples of **incorrect** code for the `{ "typeof": true }` option:
78 /*eslint no-undef: ["error", { "typeof": true }] */
80 if(typeof a === "string"){}
85 Examples of **correct** code for the `{ "typeof": true }` option with `global` declaration:
91 /*eslint no-undef: ["error", { "typeof": true }] */
93 if(typeof a === "string"){}
100 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](../user-guide/configuring/language-options#specifying-environments). A few examples are given below.
104 Examples of **correct** code for this rule with `browser` environment:
109 /*eslint no-undef: "error"*/
110 /*eslint-env browser*/
112 setTimeout(function() {
121 Examples of **correct** code for this rule with `node` environment:
126 /*eslint no-undef: "error"*/
129 var fs = require("fs");
130 module.exports = function() {
137 ## When Not To Use It
139 If explicit declaration of global variables is not to your taste.
143 This rule provides compatibility with treatment of global variables in [JSHint](http://jshint.com/) and [JSLint](http://www.jslint.com).