]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-undef.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-undef.md
1 ---
2 title: no-undef
3 layout: doc
4 rule_type: problem
5 related_rules:
6 - no-global-assign
7 - no-redeclare
8 ---
9
10
11
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).
13
14 ## Rule Details
15
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).
17
18 Examples of **incorrect** code for this rule:
19
20 ::: incorrect
21
22 ```js
23 /*eslint no-undef: "error"*/
24
25 var foo = someFunction();
26 var bar = a + 1;
27 ```
28
29 :::
30
31 Examples of **correct** code for this rule with `global` declaration:
32
33 ::: correct
34
35 ```js
36 /*global someFunction, a*/
37 /*eslint no-undef: "error"*/
38
39 var foo = someFunction();
40 var bar = a + 1;
41 ```
42
43 :::
44
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.
47
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.
50
51 ## Options
52
53 * `typeof` set to true will warn for variables used inside typeof check (Default false).
54
55 ### typeof
56
57 Examples of **correct** code for the default `{ "typeof": false }` option:
58
59 ::: correct
60
61 ```js
62 /*eslint no-undef: "error"*/
63
64 if (typeof UndefinedIdentifier === "undefined") {
65 // do something ...
66 }
67 ```
68
69 :::
70
71 You can use this option if you want to prevent `typeof` check on a variable which has not been declared.
72
73 Examples of **incorrect** code for the `{ "typeof": true }` option:
74
75 ::: incorrect
76
77 ```js
78 /*eslint no-undef: ["error", { "typeof": true }] */
79
80 if(typeof a === "string"){}
81 ```
82
83 :::
84
85 Examples of **correct** code for the `{ "typeof": true }` option with `global` declaration:
86
87 ::: correct
88
89 ```js
90 /*global a*/
91 /*eslint no-undef: ["error", { "typeof": true }] */
92
93 if(typeof a === "string"){}
94 ```
95
96 :::
97
98 ## Environments
99
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.
101
102 ### browser
103
104 Examples of **correct** code for this rule with `browser` environment:
105
106 ::: correct
107
108 ```js
109 /*eslint no-undef: "error"*/
110 /*eslint-env browser*/
111
112 setTimeout(function() {
113 alert("Hello");
114 });
115 ```
116
117 :::
118
119 ### Node.js
120
121 Examples of **correct** code for this rule with `node` environment:
122
123 ::: correct
124
125 ```js
126 /*eslint no-undef: "error"*/
127 /*eslint-env node*/
128
129 var fs = require("fs");
130 module.exports = function() {
131 console.log(fs);
132 };
133 ```
134
135 :::
136
137 ## When Not To Use It
138
139 If explicit declaration of global variables is not to your taste.
140
141 ## Compatibility
142
143 This rule provides compatibility with treatment of global variables in [JSHint](http://jshint.com/) and [JSLint](http://www.jslint.com).