no-unused-vars
eslint
SuggestionRecommended
eslint.config.js
export default [
{
rules: {
"no-unused-vars": "error",
},
},
];/*global some_unused_var*/
// It checks variables you have defined as global
some_unused_var = 42;
let x;
// Write-only variables are not considered as used.
let y = 10;
y = 5;
// A read for a modification of itself is not considered as used.
let z = 0;
z = z + 1;
// 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);
}
// When a function definition destructures an array, unused entries from the array also cause warnings.
function getY([x, y]) {
return y;
}
getY(["a", "b"]);const x = 10;
alert(x);
// foo is considered used here
myFunc(function foo() {
// ...
}.bind(this));
(function(foo) {
return foo;
})();
var myFunc;
myFunc = setTimeout(function() {
// myFunc is considered used
myFunc();
}, 50);
// Only the second argument from the destructured array is used.
function getY([, y]) {
return y;
}
getY(["a", "b"]);/* exported global_var */
var global_var = 42;Examples from the ESLint documentation.
no-unused-vars
eslint
export default [
{
rules: {
"no-unused-vars": "error",
},
},
];no-unused-vars
eslint plugin · correctness
{
"rules": {
"no-unused-vars": "error"
}
}correctness/noUnusedVariables
correctness · since 1.0.0
{
"linter": {
"rules": {
"correctness": {
"noUnusedVariables": "error"
}
}
}
}Disallow unused variable definitions of v-for directives or scope attributes
Unused local variables and functions should be removed
Require super() calls in constructors
Enforce return statements in getters