Disallow Unused Variables (no-unused-vars)

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.

Rule Details

This rule is aimed at eliminating unused variables, functions and variables in parameters of functions, as such, warns when one is found.

A variable is considered to be used when it:

  1. Represents a function that is called (doSomething())
  2. Is read (var y = x)
  3. Is passed into a function as an argument (doSomething(x))

A variable is not considered read if it is only ever assigned to (var x = 5) or declared.

The following patterns are considered problems:

/*eslint no-unused-vars: 2*/
/*global some_unsed_var */   /*error "some_unsed_var" is defined but never used*/

//It checks variables you have defined as global
some_unsed_var = 42;

var x;                       /*error "x" is defined but never used*/

var y = 10;                  /*error "y" is defined but never used*/
y = 5;

// By default, unused arguments cause warnings.
(function(foo) {             /*error "foo" is defined but never used*/
    return 5;
})();

// Unused recursive functions also cause warnings.
function fact(n) {           /*error "fact" is defined but never used*/
    if (n < 2) return 1;
    return n * fact(n - 1);
}

The following patterns are not considered problems:

/*eslint no-unused-vars: 2*/

var x = 10;
alert(x);

// foo is considered used here
myFunc(function foo() {
    // ...
}.bind(this));

(function(foo) {
    return foo;
})();

Exporting Variables

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. Note that /* exported */ has no effect when used with the node or commonjs environments or when ecmaFeatures.modules is true.

Options

By default this rule is enabled with all option for variables and after-used for arguments.

{
    "rules": {
        "no-unused-vars": [2, {"vars": "all", "args": "after-used"}]
    }
}

vars

This option has two settings:

args

This option has three settings:

with { "args": "all" }
/*eslint no-unused-vars: [2, { "args": "all" }]*/

(function(foo, bar, baz) { /*error "foo" is defined but never used*/ /*error "baz" is defined but never used*/
    return bar;
})();
with { "args": "after-used" }
/*eslint no-unused-vars: [2, { "args": "after-used" }]*/

(function(foo, bar, baz) { /*error "baz" is defined but never used*/
    return bar;
})();
with { "args": "none" }
/*eslint no-unused-vars: [2, { "args": "none" }]*/

(function(foo, bar, baz) {
    return bar;
})();

Ignore identifiers that match specific patterns

Examples

When Not to Use It

If you don’t want to be notified about unused variables or function arguments, you can safely turn this rule off.

Version

This rule was introduced in ESLint 0.0.9.

Resources