Unlike strongly typed languages, JavaScript does not enforce a return type on a function. This means that different paths through a function can
return different types of values, which can be very confusing to the user and significantly harder to maintain.
In particular a function, in JavaScript, will return undefined
in any of the following cases:
- It exits without a
return
statement.
- It executes a
return
with no value.
This rule verifies that return values are either always or never specified for each path through a function.
Noncompliant code example
function foo(a) { // Noncompliant, function exits without "return"
if (a == 1) {
return true;
}
}
Compliant solution
function foo(a) {
if (a == 1) {
return true;
}
return false;
}