79 lines
3.2 KiB
Plaintext
79 lines
3.2 KiB
Plaintext
This rule raises an issue when a strict equality operator is used to compare objects of different types.
|
|
|
|
== Why is this an issue?
|
|
|
|
The strict equality operator in JavaScript is represented by three equal signs (``++===++``), the strict inequality with (``++!==++``). It is used to compare two values for equality, but with an important difference from the regular equality operator (``++==++``). The strict equality operator compares both value and type, while the regular equality operator only compares values after performing type coercion if necessary.
|
|
|
|
The problem with using the strict equality operator (``++===++``) with operands of dissimilar types lies in the way JavaScript handles the comparison. When you use ``++===++`` to compare two values of different types, it will always return false since their types are different, regardless of whether the values could be considered equal under certain conditions.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
let a = 8;
|
|
let b = "8";
|
|
|
|
if (a === b) { // Noncompliant: Always false since 'a' is a number and 'b' a string
|
|
// ...
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
To address this issue, you can use the loose equality operator (``++==++``), which performs type coercion.
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let a = 8;
|
|
let b = "8";
|
|
|
|
if (a == b) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
Alternatively, use the strict equality operator (``++===++``) but ensure that the operands have the same type before performing the comparison. You can explicitly convert the operands to a common type using functions like ``++Number()++``, ``++String()++``, or other appropriate methods depending on the situation.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let a = 8;
|
|
let b = "8";
|
|
|
|
if (a === Number(b)) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality[Strict equality (``++===++``)]
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality[Strict inequality (``++!==++``)]
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality[Equality (``++==++``)]
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality[Inequality (``++!=++``)]
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion[Type coercion]
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number[``++Number()++`` constructor]
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String[``++String()++`` constructor]
|
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean[``++Boolean()++`` constructor]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this ["==="|"!=="] check; it will always be [false|true]. Did you mean to use ["=="|"!="]?
|
|
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|