diff --git a/rules/S1440/javascript/rule.adoc b/rules/S1440/javascript/rule.adoc index 1f826504484..d532dc95caa 100644 --- a/rules/S1440/javascript/rule.adoc +++ b/rules/S1440/javascript/rule.adoc @@ -1,20 +1,60 @@ == Why is this an issue? -include::../description.adoc[] +In JavaScript, there are two types of comparison operators: strict and non-strict. -include::../noncompliant.adoc[] +* Strict operators: These operators compare both value and type. They are represented as `===` (strict equality) and `!==` (strict inequality). For example, `5 === "5"` would return `false` because, although the values are the same, the types are different (one is a number, the other is a string). -include::../compliant.adoc[] +* Non-Strict operators: These operators compare only value, not type. They are represented as `==` (equality) and `!=` (inequality). For example, `5 == "5"` would return `true` because the values are the same, even though the types are different. -=== Exceptions +It's generally recommended to use strict operators in JavaScript to avoid unexpected results due to JavaScript's type coercion. This is because non-strict operators can lead to some counter-intuitive results. For example, `0 == false` would return `true`, which might not be the expected outcome. + +[source,javascript,diff-id=1,diff-type=noncompliant] +---- +function checkEqual(a, b) { + if (a == b) { // Noncompliant: using non-strict equality '==' + return "Equal"; + } else { + return "Not equal"; + } +} -Even if testing the equality of a variable against null doesn't do exactly what most JavaScript developers believe, usage of ``++==++`` or ``++!=++`` is tolerated in such context. In the following case, if ``++foo++`` hasn't been initialized, its default value is not ``++null++`` but ``++undefined++``. Nevertheless ``++undefined == null++``, so JavaScript developers get the expected behavior. +console.log(checkEqual(0, false)); // Output: "Equal" +---- + +You should use the strict equality and inequality operators to prevent type coercion, avoid unexpected outcomes when comparing values of different types, and provide more predictable results. -[source,javascript] +[source,javascript,diff-id=1,diff-type=compliant] ---- -if(foo == null) {...} +function checkEqual(a, b) { + if (a === b) { + return "Equal"; + } else { + return "Not equal"; + } +} + +console.log(checkEqual(0, false)); // Output: "Not equal ---- +=== Exceptions + +The rule does not report on these cases: + +* Comparing two literal values +* Evaluating the value of `typeof` +* Comparing against `null` + +== Resources +=== Documentation + +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality[MDN - Strict equality] +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality[MDN - Strict inequality] +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality[MDN - Equality] +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality[MDN - Inequality] +* https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion[MDN - Type coercion] +* https://developer.mozilla.org/en-US/docs/Glossary/Truthy[MDN - Truthy] +* https://developer.mozilla.org/en-US/docs/Glossary/Falsy[MDN - Falsy] + ifdef::env-github,rspecator-view[] '''