79 lines
3.2 KiB
Plaintext
Raw Normal View History

2023-08-04 16:46:24 +02:00
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.
2020-06-30 12:48:39 +02:00
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.
2020-06-30 12:48:39 +02:00
2023-08-04 16:46:24 +02:00
=== Code examples
==== Noncompliant code example
[source,javascript,diff-id=1,diff-type=noncompliant]
2020-06-30 12:48:39 +02:00
----
let a = 8;
let b = "8";
2020-06-30 12:48:39 +02:00
if (a === b) { // Noncompliant: Always false since 'a' is a number and 'b' a string
2020-06-30 12:48:39 +02:00
// ...
}
----
2023-08-04 16:46:24 +02:00
==== 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]
2020-06-30 12:48:39 +02:00
----
let a = 8;
let b = "8";
2020-06-30 12:48:39 +02:00
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]
2020-06-30 12:48:39 +02:00
----
let a = 8;
let b = "8";
2020-06-30 12:48:39 +02:00
if (a === Number(b)) {
// ...
}
----
== Resources
=== Documentation
2023-08-04 16:46:24 +02:00
* 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[]