42 lines
1019 B
Plaintext
42 lines
1019 B
Plaintext
``++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
|
|
|
|
----
|
|
function func1(p1, p2) {
|
|
|
|
if (p1 === null) { // Noncompliant
|
|
return;
|
|
}
|
|
if (p1 === undefined) { // Noncompliant
|
|
return;
|
|
}
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
function func1(p1, p2) {
|
|
|
|
if (p1 == null) { // true for null, undefined
|
|
return;
|
|
}
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::rspecator-view[]
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::rspecator-view[]
|