Why is this an issue?

An unused local variable or function can be leftover code, or a sign that the wrong identifier is being used. Remove it when it is no longer needed; otherwise, restore its intended use. This makes it clear which code contributes to the result.

Code examples

Remove a declaration that has no effect on the calculation.

Noncompliant code example

function numberOfMinutes(hours) {
  const seconds = 0; // Noncompliant: "seconds" is never used
  return hours * 60;
}

Compliant solution

function numberOfMinutes(hours) {
  return hours * 60;
}

When destructuring an array, keep the position of the values you use. An empty slot skips a value without declaring a variable.

Noncompliant code example

function getQuery(url) {
  const [path, query] = url.split("?"); // Noncompliant: "path" is never used
  return query;
}

Compliant solution

function getQuery(url) {
  const [, query] = url.split("?");
  return query;
}

Preserve necessary side effects

An unused variable’s initializer still runs. Before deleting its declaration, check whether that expression performs required work. For example, replace const result = saveChanges(); with saveChanges(); if the call is needed but its return value is not.

Resources

Documentation

Articles & blog posts