This rule raises an issue when a function parameter is reassigned using the logical OR operator (||) with a literal value as the
fallback.
Reassigning function parameters using the logical OR operator (||) creates different behavior than using default parameters, which can
lead to unexpected results.
When you write foo = foo || 'default', the fallback value is used whenever foo is any falsy value (including
null, 0, false, empty string '', etc.). This means legitimate values like 0 or
false get replaced with the fallback.
Default parameters, on the other hand, only apply when the parameter is specifically undefined. This is usually the intended behavior
when providing fallback values.
Default parameters also make the function signature clearer by showing the expected defaults upfront.
This pattern can cause bugs when legitimate falsy values (like 0, false, or empty strings) are passed as arguments but
get replaced with the fallback value. This is especially problematic in functions that handle numeric values, boolean flags, or optional strings where
these falsy values have meaning.
Replace parameter reassignment with default parameter syntax. This ensures the default value is only used when the parameter is
undefined, not when it’s other falsy values.
function setWidth(width) {
width = width || 100; // Problem: width=0 becomes 100
// ...
}
setWidth(0); // Unexpectedly uses 100 instead of 0
function setWidth(width = 100) {
// width=0 stays 0, only undefined becomes 100
}
setWidth(0); // Correctly uses 0
setWidth(); // Uses default 100