redline
LowType safetyloose-equality

Loose equality

Loose equality. == coerces types before comparing, so 0 == '' is true.

This is how the comment reads inline, under the flagged line.

Why it matters

Loose equality follows coercion rules few people remember. It hides type bugs.

What to do

Use === and !==. == null is the one common exception (null or undefined).

Examples

Flagged
if (count == '0') {}
Better
if (count === 0) {}

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

if (a == b) {}
if (a != 1) {}

Left alone

if (a === b) {}
if (a == null) {}

Research

`==`, `var`, `== None`, and `is` with a literal. Coercion and leaky scope make comparisons true or false for the wrong reason.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "loose-equality"
    ],
    "severity": {
      "loose-equality": "medium"
    }
  }
}

Read the source or report a false positive.