Skip to content

noUselessAssignment (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUselessAssignment": "error"
}
}
}
}

Disallow assignments whose value is never read.

If a variable gets a value that is never read before the variable is assigned again, or before the variable stops being used, the assignment has no effect. This often hints at a mistake: the value may have been meant to be used, or to be assigned to a different variable.

let id = "x1234"; // this value is never read
id = generateId();
doSomethingWith(id);
code-block.js:1:5 lint/nursery/noUselessAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The value assigned to id is never read.

> 1 │ let id = "x1234"; // this value is never read
│ ^^
2 │ id = generateId();
3 │ doSomethingWith(id);

ℹ After this assignment, the variable is either assigned again or not read anymore, so this value is thrown away.

ℹ Remove the assignment if it's unnecessary. Otherwise, check whether the value should be used, or assigned to a different variable.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

The rule checks every way the code can run, including loops, break, continue, return, and errors caught by try statements.

Variables that are never read are reported by noUnusedVariables instead. The rule also ignores variables that are used inside nested functions, because it can’t know when those functions run. Exported variables and top-level variables of Astro, Svelte, and Vue files are ignored too, because other files or the component template can read them.

function fn() {
let v = "used";
doSomething(v);
v = "unused";
}
code-block.js:4:5 lint/nursery/noUselessAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The value assigned to v is never read.

2 │ let v = "used";
3 │ doSomething(v);
> 4 │ v = "unused";
│ ^
5 │ }
6 │

ℹ After this assignment, the variable is either assigned again or not read anymore, so this value is thrown away.

ℹ Remove the assignment if it's unnecessary. Otherwise, check whether the value should be used, or assigned to a different variable.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function fn() {
let v = "used";
if (condition) {
v = "unused";
return;
}
doSomething(v);
}
code-block.js:4:9 lint/nursery/noUselessAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The value assigned to v is never read.

2 │ let v = "used";
3 │ if (condition) {
> 4 │ v = "unused";
│ ^
5 │ return;
6 │ }

ℹ After this assignment, the variable is either assigned again or not read anymore, so this value is thrown away.

ℹ Remove the assignment if it's unnecessary. Otherwise, check whether the value should be used, or assigned to a different variable.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function fn() {
let v = "unused";
v = "used";
doSomething(v);
}
code-block.js:2:9 lint/nursery/noUselessAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The value assigned to v is never read.

1 │ function fn() {
> 2 │ let v = "unused";
│ ^
3 │ v = "used";
4 │ doSomething(v);

ℹ After this assignment, the variable is either assigned again or not read anymore, so this value is thrown away.

ℹ Remove the assignment if it's unnecessary. Otherwise, check whether the value should be used, or assigned to a different variable.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function fn() {
let count = 0;
doSomething(count);
count++;
}
code-block.js:4:5 lint/nursery/noUselessAssignment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The value assigned to count is never read.

2 │ let count = 0;
3 │ doSomething(count);
> 4 │ count++;
│ ^^^^^
5 │ }
6 │

ℹ After this assignment, the variable is either assigned again or not read anymore, so this value is thrown away.

ℹ Remove the assignment if it's unnecessary. Otherwise, check whether the value should be used, or assigned to a different variable.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function fn1() {
let v = "used";
doSomething(v);
v = "used-2";
doSomething(v);
}
function fn2() {
let v = "used";
for (let i = 0; i < 10; i++) {
doSomething(v);
v = "used in next iteration";
}
}
function fn3() {
let result = "fallback";
try {
result = compute();
} catch {
// `result` keeps its previous value when `compute()` throws.
}
return result;
}