Loose equality
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
if (count == '0') {}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"
}
}
}
==coerces types before comparing, so0 == ''is true.