Does every Javascript function have to return a value?

JavascriptReturn ValueNetbeans 7

Javascript Problem Overview


I'm using Netbeans to add professional-like comments to each function, I write. So I begin each of it with /** and then I press Enter to let Netbeans fulfill default comment scheme for following function.

Up until now I've been using this only for PHP language and in this case Netbeans was always adding @returns {type} part in comment scheme only, if following PHP function really included return statement. On so called "procedures" (functions that does not return any value) this part was missing.

Today I tried the same thing for Javascript function and Netbeans added @returns {undefined} part to comment scheme even though following function does not return anything.

This confused me. Does Netbeans suggests this way, that every Javascript function has to return anything? What should I do? Ignore (or delete) that comment scheme part or follow suggestion (if this is suggestion at all) and add return false; in the end of such function, though it is useless for me?

Javascript Solutions


Solution 1 - Javascript

The short answer is no.

The real answer is yes: the JS engine has to be notified that some function has finished its business, which is done by the function returning something. This is also why, instead of "finished", a function is said to "have returned".
A function that lacks an explicit return statement will return undefined, like a C(++) function that has no return value is said (and its signature reflects this) to return void:

void noReturn()//return type void
{
    printf("%d\n", 123);
    return;//return nothing, can be left out, too
}

//in JS:
function noReturn()
{
    console.log('123');//or evil document.write
    return undefined;//<-- write it or not, the result is the same
    return;//<-- same as return undefined
}

Also, in JS, like in most every language, you're free to simply ignore the return value of a function, which is done an awful lot:

(function()
{
    console.log('this function in an IIFE will return undefined, but we don\'t care');
}());
//this expression evaluates to:
(undefined);//but we don't care

At some very low level, the return is translated into some sort of jump. If a function really returned nothing at all, there would be no way of knowing what and when to call the next function, or to call event handlers and the like.

So to recap: No, a JS function needn't return anything as far as your code goes. But as far as the JS engines are concerned: a function always returns something, be it explicitly via a return statement, or implicitly. If a function returns implicitly, its return value will always be undefined.

Solution 2 - Javascript

No, return is not necessary.

When no return statement is specified, undefined is returned.

Solution 3 - Javascript

> Does every Javascript function have to return a value?

No, they don't. It's true that deep in the specification, these are all slightly different:

function foo() {
}
function foo() {
    return;
}
function foo() {
    return undefined;
}

...but the result of calling each of them is the same: undefined. So in pragmatic terms:

  1. You don't have to write a return, you can just let code execution "fall off the end" of the function
  2. If you're returning undefined, specifically, you an just write return;
  3. When calling a function, you cannot tell (in code) whether execution fell off the end, ended with return;, or ended with return undefined;; they all look exactly the same to your calling code

Re the spec: Specifically, when a function's execution falls off the end, in the spec that's a "normal" completion; but return; and return value; are both "return" completions with an associated value (undefined), which is (ever so slightly) different. But the difference is eliminated by the semantics of calling a function, which say:

> ... > > 9. If result.[[Type]] is return, return NormalCompletion(result.[[Value]]). > 10. ReturnIfAbrupt(result). > 11. Return NormalCompletion(undefined).

So there's no difference you can observe in code.

Solution 4 - Javascript

No, you don't have to return something for every function. It is optional and upto how you write your code logic.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontrejderView Question on Stackoverflow
Solution 1 - JavascriptElias Van OotegemView Answer on Stackoverflow
Solution 2 - JavascriptrhapsodynView Answer on Stackoverflow
Solution 3 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 4 - JavascriptmohkhanView Answer on Stackoverflow