57 lines
1.6 KiB
Plaintext
57 lines
1.6 KiB
Plaintext
This rule raises an issue when a function call result is used, even though the function does not return anything.
|
|
|
|
== Why is this an issue?
|
|
|
|
When a function in JavaScript does not have a return statement or if it has a return statement without a value, it implicitly returns `undefined`. This means that a function without a return statement or with an empty return statement is, in a way, a "void" function, as it doesn't return any specific value.
|
|
|
|
Therefore, attempting to use the return value of a void function in JavaScript is meaningless, and it can lead to unexpected behavior or errors.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
function foo() {
|
|
console.log("Hello, World!");
|
|
}
|
|
|
|
let a = foo(); // Noncompliant: Assigning the return value of a void function
|
|
----
|
|
|
|
You should not use in any way the return value of a void function.
|
|
|
|
==== Compliant solution
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
function foo() {
|
|
console.log("Hello, World!");
|
|
}
|
|
|
|
foo();
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined[``++undefined++`` global property]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return[``++return++`` statement]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|