Skip to content

noUnusedVariables

Disallow unused variables.

There is an exception to this rule: variables that starts with underscore, e.g. let _something;.

The pattern of having an underscore as prefix of a name of variable is a very diffuse pattern among programmers, and Biome decided to follow it.

This rule won’t report unused imports. If you want to report unused imports, enable noUnusedImports.

The rule supports the following options:

{
"options": {
"ignoreRestSiblings": true
}
}
  • ignoreRestSiblings: Whether to ignore unused variables from an object destructuring with a spread (i.e.: whether a and b in const { a, b, ...rest } = obj should be ignored by this rule). Defaults to false.
let a = 4;
a++;
code-block.js:1:5 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable is unused.

> 1 │ let a = 4;
^
2 │ a++;
3 │

Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.

Unsafe fix: If this is intentional, prepend a with an underscore.

1 - let·a·=·4;
2 - a++;
1+ let·_a·=·4;
2+ _a++;
3 3

function foo() {}
code-block.js:1:10 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This function is unused.

> 1 │ function foo() {}
^^^
2 │

Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.

Unsafe fix: If this is intentional, prepend foo with an underscore.

1 - function·foo()·{}
1+ function·_foo()·{}
2 2

function foo() {
foo();
}
code-block.js:1:10 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This function is unused.

> 1 │ function foo() {
^^^
2 │ foo();
3 │ }

Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.

Unsafe fix: If this is intentional, prepend foo with an underscore.

1 - function·foo()·{
2 - ····foo();
1+ function·_foo()·{
2+ ····_foo();
3 3 }
4 4

const foo = () => {
foo();
};
code-block.js:1:7 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable is unused.

> 1 │ const foo = () => {
^^^
2 │ foo();
3 │ };

Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.

Unsafe fix: If this is intentional, prepend foo with an underscore.

1 - const·foo·=·()·=>·{
2 - ····foo();
1+ const·_foo·=·()·=>·{
2+ ····_foo();
3 3 };
4 4

export function f<T>() {}
code-block.ts:1:19 lint/correctness/noUnusedVariables  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This type parameter is unused.

> 1 │ export function f<T>() {}
^
2 │

Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.

Unsafe fix: If this is intentional, prepend T with an underscore.

1 - export·function·f<T>()·{}
1+ export·function·f<_T>()·{}
2 2

// With `ignoreRestSiblings: false`
const car = { brand: "Tesla", year: 2019, countryCode: "US" };
const { brand, ...other } = car;
console.log(other);
code-block.js:3:9 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This variable is unused.

1 │ // With ignoreRestSiblings: false
2 │ const car = { brand: “Tesla”, year: 2019, countryCode: “US” };
> 3 │ const { brand, …other } = car;
^^^^^
4 │ console.log(other);
5 │

Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.

You can use the ‘ignoreRestSiblings’ option to ignore unused variables in an object destructuring with a spread.

function foo(b) {
console.log(b)
};
foo();
export function foo(_unused) {}
function used_overloaded(): number;
function used_overloaded(s: string): string;
function used_overloaded(s?: string) {
return s;
}
used_overloaded();
// With `ignoreRestSiblings: false`
const car = { brand: "Tesla", year: 2019, countryCode: "US" };
const { brand: _brand, ...other } = car;
console.log(other);
// With `ignoreRestSiblings: true`
const car = { brand: "Tesla", year: 2019, countryCode: "US" };
const { brand, ...other } = car;
console.log(other);
biome.json
{
"linter": {
"rules": {
"correctness": {
"noUnusedVariables": "error"
}
}
}
}