<qhelp> <overview>

In JavaScript, equality operators (`==`, `!=`, `===`, `!==`) and relational operators (`<`, `<=`, `>`, `>=`) can be applied to values of arbitrary types. However, if the operands cannot be converted to a common type, the result of the comparison will always be trivially true (for equality) or false (for inequality). Such comparisons are often due to a typo or a misunderstanding of the language semantics.

</overview> <recommendation>

Inspect the comparison carefully to check whether it is due to a typo. If one of the operands is a constant, replace it with a constant of the right type. Otherwise, introduce appropriate function calls to convert the operands to a common type.

</recommendation> <example>

The following code attempts to check whether the global variable `window` is defined:

However, this test is ineffective: `typeof` always returns a string, never `undefined`, so the `if` condition will always evaluate to `true`. Instead, the result of `typeof` should be compared to the string literal `"undefined"`:

As another example, consider the following code snippet, which is meant to check whether the string `"hello"` occurs in the string held in variable `text`.

Note, however, that the test has been mistyped: the closing parenthesis of the call to `index` should come before the operator `>=`, not after it. As it stands, this code performs a greater-or-equal comparison between the constant string `"hello"` and the number zero, which evaluates to `false`. This value is then passed to `indexOf`, which converts it to the string `"false"` and returns the first index at which this string occurs in `text` (or `-1` if it does not occur at all).

To fix this issue, the test should be rebracketed like this:

</example> <references>

- Mozilla Developer Network: Comparison Operators.
- Mozilla Developer Network: Equality comparisons and when to use them.
</references> </qhelp>