Unused Local Variable
ID |
javascript.unused_local_variable |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
CWE:563, code-style, dead-code |
Description
Reports var, let or const declarations whose binding is never referenced after declaration. Plain identifier bindings only — destructuring patterns are out of scope for this version of the rule.
Rationale
// Bad — declared and forgotten.
function dead() {
const cache = new Map();
return 42;
}
// Bad — refactor leftover.
function processed(rows) {
const total = rows.length;
return rows.map(format);
}
Unused bindings clutter scope, mislead readers about what the function uses, and often hide that an earlier branch was deleted but its setup wasn’t.
Remediation
Delete the declaration. When the right-hand side has a side effect that the function still needs, hoist that call to a statement of its own:
// Good — keep the side effect, drop the unused binding.
function withSideEffect() {
cleanupQueue(); // was: const cleaned = cleanupQueue();
return 42;
}
Exclusions:
-
Names starting with
_are treated as intentionally unused (placeholder pattern). -
Declarations inside an
exportare consumed by other modules — they are not flagged even when no local code references them.