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.

The following patterns are considered warnings:


//It checks variables you have defined as global
/*global some_used_var */

var x = 10;

var y = 10;
y = 5;

// By default, unused arguments cause warnings.
(function(foo) {
    return 5;
})();

// Unused recursive functions also cause warnings.
function fact(n) {
    if (n < 2) return 1;
    return n * fact(n - 1);
}

The following patterns are not considered warnings:

var x = 10;
alert(x);

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

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

Exporting Variables

In some environments 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 may be used elsewhere.

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:

The following code:

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

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