== Why is this an issue?

If a local variable or a local function is declared but not used, it is dead code and should be removed. Doing so will improve maintainability because developers will not wonder what the variable or function is used for.

=== What is the potential impact?

==== Dead code
An unused variable or local function usually occurs because some logic is no longer required after a code change. In that case, such code becomes unused and never executed. 

Also, if you are writing code for the front-end, every unused variable or function remaining in your codebase is just extra bytes you have to send over the wire to your users. Unused code bloats your codebase unnecessarily and impacts the performance of your application.

==== Wrong logic
It could happen that due to a bad copy-paste or autocompletion, the wrong variable is used, while the right one is only declared. In that case, the unused variable should be used instead of deleted from the codebase.

==== Memory leaks
Finally, unused functions can also cause memory leaks. For example, an unused function can create a closure over a variable that would otherwise be released to the garbage collector.

[source,javascript]
----
let theThing = null;
const replaceThing = function () {
  const originalThing = theThing;
  const unused = function () {
    if (originalThing) {
      console.log("hi");
    }
  };
  theThing = {
    longStr: new Array(1000000).join("*"),
    someMethod: function () {
      console.log(someMessage);
    },
  };
};
setInterval(replaceThing, 1000);
----


== How to fix it

Usually, the fix for this issue is straightforward, you just need to remove the unused variable declaration, or its name from the declaration statement if it is declared along with other variables.

=== Code examples

==== Noncompliant code example

[source,javascript,diff-id=1,diff-type=noncompliant]
----
function numberOfMinutes(hours) {
  var seconds = 0;   // seconds is never used
  return hours * 60;
}
----

==== Compliant solution

[source,javascript,diff-id=1,diff-type=compliant]
----
function numberOfMinutes(hours) {
  return hours * 60;
}
----

==== Noncompliant code example

When an array destructuring is used and some element of the array is never referenced, one might simply remove it from the destructuring. 

[source,javascript,diff-id=2,diff-type=noncompliant]
----
const [_, params] = url.split(path);
----

==== Compliant solution

[source,javascript,diff-id=2,diff-type=compliant]
----
const [, params] = url.split(path);
----

== Resources

=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#ignoring_some_returned_values[Destructuring assignment / Ignoring some values]


=== Articles & blog posts
* https://www.sonarsource.com/blog/common-typescript-issues-no-3-unused-local-variables-and-functions/[Phil Nash, Common TypeScript Issues Nº 3: unused local variables and functions]
* https://blog.meteor.com/an-interesting-kind-of-javascript-memory-leak-8b47d2e7f156[
David Glasser, An interesting kind of JavaScript memory leak]

// internal data
ifdef::env-github,rspecator-view[]

'''
== Implementation specification
(visible only on this page)

=== Message

Remove unused function 'xxx'.

Remove the declaration of the unused 'xxx' variable.


'''
== Comments and links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
