Aller au contenu

noDoubleEquals

Ce contenu n’est pas encore disponible dans votre langue.

Diagnostic Category: lint/suspicious/noDoubleEquals

Since: v1.0.0

Sources:

Require the use of === and !==.

It is generally bad practice to use == for comparison instead of ===. Double operators will trigger implicit type coercion and are thus not preferred. Using strict equality operators is almost always best practice.

For ergonomic reasons, this rule makes by default an exception for == null for comparing to both null and undefined.

foo == bar
code-block.js:1:5 lint/suspicious/noDoubleEquals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use === instead of ==

> 1 │ foo == bar
^^
2 │

== is only allowed when comparing against null

> 1 │ foo == bar
^^
2 │

Using == may be unsafe if you are relying on type coercion

Unsafe fix: Use ===

1 │ foo·===·bar
+
foo == null
foo != null
null == foo
null != foo

The rule provides the option described below.

{
"//":"...",
"options": {
"ignoreNull": true
}
}

When this option is set to true, an exception will be made for checking against null, as relying on the double equals operator to compare with null is frequently used to check equality with either null or undefined.

When the option is set to false, all double equal operators will be forbidden without exceptions.

Default: true