59 lines
1.3 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
``++null++`` and ``++undefined++`` are similar but not synonymous concepts in Javascript. Use a "hard" test (``++===++`` or ``++!==++``) for one, and you'll miss the other. Soft tests (``++==++``, and ``++!=++``) on the other hand, will pick up both non-values, as well as empty string.
Even if you mean to make the distinction in your code between ``++null++`` and ``++undefined++``, doing so is an extremely questionable practice that is likely to confuse both users and maintainers. Instead, use one or the other and test for both using soft tests.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
function func1(p1, p2) {
if (p1 === null) { // Noncompliant
return;
}
if (p1 === undefined) { // Noncompliant
return;
}
// ...
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
function func1(p1, p2) {
if (p1 == null) { // true for null, undefined
return;
}
// ...
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use the "xx" operator instead.
'''
== Comments And Links
(visible only on this page)
=== on 13 Nov 2015, 14:58:00 Linda Martin wrote:
\[~elena.vilchik] I think this rule requires some digging, I let you do that.
endif::env-github,rspecator-view[]