54 lines
1.2 KiB
Plaintext
54 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
``++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
|
|
|
|
[source,javascript]
|
|
----
|
|
function func1(p1, p2) {
|
|
|
|
if (p1 === null) { // Noncompliant
|
|
return;
|
|
}
|
|
if (p1 === undefined) { // Noncompliant
|
|
return;
|
|
}
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
function func1(p1, p2) {
|
|
|
|
if (p1 == null) { // true for null, undefined
|
|
return;
|
|
}
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|