Skip to content

noDoubleEquals

诊断类别:lint/suspicious/noDoubleEquals

¥Diagnostic Category: lint/suspicious/noDoubleEquals

自从:v1.0.0

¥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.

出于人机工程学原因,此规则默认将 == nullnullundefined 进行比较。

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

¥Examples

¥Invalid

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
+

¥Valid

foo == null
foo != null
null == foo
null != foo

¥Options

规则提供下面描述的选项。

¥The rule provides the option described below.

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

当此选项设置为 true 时,将对 null 进行检查,因为依赖双等号运算符与 null 进行比较通常用于检查与 nullundefined 的相等性。

¥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.

当选项设置为 false 时,所有双等号运算符都将被禁止,无一例外。

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

默认:true

¥Default: true

¥Related links