quick-lint-js

Find bugs in JavaScript programs.

E0190: missing comparison; '===' does not extend to the right side of '||'

In English, we might say “if the selection is ‘agree’ or ‘strongly agree’, return a rating of 1”. If this English phrase is translated naïvely into JavaScript, the code will behave as if every selection has a rating of 1:

function getRating(radioElement) {
  let selection = radioElement.value;
  if (selection === "agree" || "strongly agree") {
    return 1;
  } else if (selection === "disagree") {
    return 0;
  } else if (selection === "strongly disagree") {
    return -1;
  }
}

This happens because if (selection === "agree" || "strongly agree") is interpreted as if ((selection === "agree") || "strongly agree"), i.e. “if selection is ‘agree’, or if ‘strongly agree’ is truthy, then …”. Because the string "strongly agree" is not empty, it is truthy, and the condition is always true.

To fix this error, write the == or === comparison on both sides of ||:

function getRating(radioElement) {
  let selection = radioElement.value;
  if (selection === "agree" ||
      selection === "strongly agree") {
    return 1;
  } else if (selection === "disagree") {
    return 0;
  } else if (selection === "strongly disagree") {
    return -1;
  }
}

Note: As of quick-lint-js version 2.11.0, this diagnostic treats undefined as a constant. If you declare your own variable named undefined, E0190 might be incorrectly reported. To work around this issue, rename your variable to something other than undefined, such as isUndefined.

Introduced in quick-lint-js version 2.0.0.

Documentation for other errors