Fix Common ESLint Warnings in Existing JavaScript Code (Part 2)
This article explains why ESLint’s no‑return‑assign and no‑constant‑condition rules fire, shows concrete examples—including arrow functions and leftover debug code—and provides step‑by‑step fixes to make the intent of the JavaScript code clear and maintainable.
ESLint is a widely used JavaScript linting tool that reports errors when code violates its rules. This article focuses on two rules that require manual fixes: no-return-assign and no-constant-condition.
no-return-assign
The rule forbids returning an assignment expression because it often masks a typo. Example:
function foobar(bar) {
return foo = bar + 1;
}The code returns bar + 1 but the author likely intended to compare foo === bar + 1. When the rule triggers, decide the intended behavior and apply one of the following fixes:
Separate the assignment and the return:
foo = bar + 1;
return foo;Return the comparison directly: return foo === bar + 1; In ES6 arrow functions, the same rule can be triggered unintentionally. For a delayed navigation: setTimeout(() => location.href = myUrl, 1000); Because the arrow function is shorthand for:
() => {
return location.href = myUrl;
}the rule flags the implicit return‑assignment. The correct version removes the return value while keeping the side effect:
() => {
location.href = myUrl;
}or, using the original one‑liner syntax:
setTimeout(() => { location.href = myUrl }, 1000);no-constant-condition
This rule disallows constant expressions in if, while, or for conditions, such as if (true) {...}. Although it seems obvious, many codebases contain such patterns for three common reasons.
Reason 1: Forgotten debug code
During debugging a condition may be temporarily forced true:
if (true || foo === bar) {
// launch feature
}If the true || part is not removed before release, the rule catches it.
Reason 2: Temporary feature toggle
Developers sometimes disable a feature by writing:
if (false && foo === bar) {
// launch feature
}A clearer approach is to introduce a flag variable:
let flag = false; // temporary disable
if (flag && foo === bar) {
// launch feature
}This eliminates the constant condition and improves readability.
Reason 3: while(true) loops
Some code uses an infinite while (true) loop with a break inside:
while (true) {
// do something
foo++;
if (foo >= bar) break;
}Because the condition is constant, the rule flags it. The loop can be rewritten as a do/while or a for loop that expresses the termination condition explicitly:
do {
// do something
foo++;
} while (foo < bar);or
for (; foo < bar; foo++) {
// do something
}If an infinite loop truly fits the logic, replace while (true) with for (;;) to silence the rule while preserving behavior.
These examples illustrate how to interpret the ESLint warnings, understand the underlying intent, and apply precise code changes without sacrificing readability or functionality.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
