1 # Disallow Unused Variables (no-unused-vars)
3 Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers.
7 This rule is aimed at eliminating unused variables, functions, and function parameters.
9 A variable `foo` is considered to be used if any of the following are true:
11 * It is called (`foo()`) or constructed (`new foo()`)
12 * It is read (`var bar = foo`)
13 * It is passed into a function as an argument (`doSomething(foo)`)
14 * It is read inside of a function that is passed to another function (`doSomething(function() { foo(); })`)
16 A variable is *not* considered to be used if it is only ever declared (`var foo = 5`) or assigned to (`foo = 7`).
18 Examples of **incorrect** code for this rule:
21 /*eslint no-unused-vars: "error"*/
22 /*global some_unused_var*/
24 // It checks variables you have defined as global
29 // Write-only variables are not considered as used.
33 // A read for a modification of itself is not considered as used.
37 // By default, unused arguments cause warnings.
42 // Unused recursive functions also cause warnings.
45 return n * fact(n - 1);
48 // When a function definition destructures an array, unused entries from the array also cause warnings.
49 function getY([x, y]) {
54 Examples of **correct** code for this rule:
57 /*eslint no-unused-vars: "error"*/
62 // foo is considered used here
63 myFunc(function foo() {
72 myFunc = setTimeout(function() {
73 // myFunc is considered used
77 // Only the second argument from the destructured array is used.
78 function getY([, y]) {
85 In environments outside of CommonJS or ECMAScript modules, you may use `var` to create a global variable that may be used by other scripts. You can use the `/* exported variableName */` comment block to indicate that this variable is being exported and therefore should not be considered unused.
87 Note that `/* exported */` has no effect for any of the following:
89 * when the environment is `node` or `commonjs`
90 * when `parserOptions.sourceType` is `module`
91 * when `ecmaFeatures.globalReturn` is `true`
93 The line comment `// exported variableName` will not work as `exported` is not line-specific.
95 Examples of **correct** code for `/* exported variableName */` operation:
98 /* exported global_var */
105 This rule takes one argument which can be a string or an object. The string settings are the same as those of the `vars` property (explained below).
107 By default this rule is enabled with `all` option for variables and `after-used` for arguments.
112 "no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
119 The `vars` option has two settings:
121 * `all` checks all variables for usage, including those in the global scope. This is the default setting.
122 * `local` checks only that locally-declared variables are used but will allow global variables to be unused.
126 Examples of **correct** code for the `{ "vars": "local" }` option:
129 /*eslint no-unused-vars: ["error", { "vars": "local" }]*/
130 /*global some_unused_var */
132 some_unused_var = 42;
135 ### varsIgnorePattern
137 The `varsIgnorePattern` option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain `ignored` or `Ignored`.
139 Examples of **correct** code for the `{ "varsIgnorePattern": "[iI]gnored" }` option:
142 /*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/
144 var firstVarIgnored = 1;
146 console.log(secondVar);
151 The `args` option has three settings:
153 * `after-used` - unused positional arguments that occur before the last used argument will not be checked, but all named arguments and all positional arguments after the last used argument will be checked.
154 * `all` - all named arguments must be used.
155 * `none` - do not check arguments.
157 #### args: after-used
159 Examples of **incorrect** code for the default `{ "args": "after-used" }` option:
162 /*eslint no-unused-vars: ["error", { "args": "after-used" }]*/
164 // 2 errors, for the parameters after the last used parameter (bar)
165 // "baz" is defined but never used
166 // "qux" is defined but never used
167 (function(foo, bar, baz, qux) {
172 Examples of **correct** code for the default `{ "args": "after-used" }` option:
175 /*eslint no-unused-vars: ["error", {"args": "after-used"}]*/
177 (function(foo, bar, baz, qux) {
184 Examples of **incorrect** code for the `{ "args": "all" }` option:
187 /*eslint no-unused-vars: ["error", { "args": "all" }]*/
190 // "foo" is defined but never used
191 // "baz" is defined but never used
192 (function(foo, bar, baz) {
199 Examples of **correct** code for the `{ "args": "none" }` option:
202 /*eslint no-unused-vars: ["error", { "args": "none" }]*/
204 (function(foo, bar, baz) {
209 ### ignoreRestSiblings
211 The `ignoreRestSiblings` option is a boolean (default: `false`). Using a [Rest Property](https://github.com/tc39/proposal-object-rest-spread) it is possible to "omit" properties from an object, but by default the sibling properties are marked as "unused". With this option enabled the rest property's siblings are ignored.
213 Examples of **correct** code for the `{ "ignoreRestSiblings": true }` option:
216 /*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/
217 // 'foo' and 'bar' were ignored because they have a rest property sibling.
218 var { foo, ...coords } = data;
221 ({ bar, ...coords } = data);
224 ### argsIgnorePattern
226 The `argsIgnorePattern` option specifies exceptions not to check for usage: arguments whose names match a regexp pattern. For example, variables whose names begin with an underscore.
228 Examples of **correct** code for the `{ "argsIgnorePattern": "^_" }` option:
231 /*eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]*/
233 function foo(x, _y) {
241 The `caughtErrors` option is used for `catch` block arguments validation.
245 * `none` - do not check error objects. This is the default setting.
246 * `all` - all named arguments must be used.
248 #### caughtErrors: none
250 Not specifying this rule is equivalent of assigning it to `none`.
252 Examples of **correct** code for the `{ "caughtErrors": "none" }` option:
255 /*eslint no-unused-vars: ["error", { "caughtErrors": "none" }]*/
260 console.error("errors");
264 #### caughtErrors: all
266 Examples of **incorrect** code for the `{ "caughtErrors": "all" }` option:
269 /*eslint no-unused-vars: ["error", { "caughtErrors": "all" }]*/
272 // "err" is defined but never used
276 console.error("errors");
280 ### caughtErrorsIgnorePattern
282 The `caughtErrorsIgnorePattern` option specifies exceptions not to check for usage: catch arguments whose names match a regexp pattern. For example, variables whose names begin with a string 'ignore'.
284 Examples of **correct** code for the `{ "caughtErrorsIgnorePattern": "^ignore" }` option:
287 /*eslint no-unused-vars: ["error", { "caughtErrorsIgnorePattern": "^ignore" }]*/
291 } catch (ignoreErr) {
292 console.error("errors");
296 ## When Not To Use It
298 If you don't want to be notified about unused variables or function arguments, you can safely turn this rule off.