123

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?

trejder
  • 17,148
  • 27
  • 124
  • 216
  • 9
    If no return value is specified, JavaScript will return `undefined`. In many languages the result of the last statement is returned (more useful, IMO). These are called *implicit returns*. – Zaz May 20 '15 at 23:24
  • 2
    Does this answer your question? [Do I have to return something in javascript function?](https://stackoverflow.com/questions/6221784/do-i-have-to-return-something-in-javascript-function) – cOborski Jan 15 '20 at 14:44

4 Answers4

222

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.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 7
    I don't believe C++ (or C) "returns a void". The "void" tag indicates it does not return anything. It's a minor point though and has nothing to do with the question asked. – Jay Mar 03 '15 at 17:02
  • 3
    It doesn't _actually_ return `void`, it returns nothing, but its signature reflects this through the `void` return type. – Elias Van Ootegem Mar 04 '15 at 08:16
  • are there any performance benefits for a explicit return vs. implicit? – 4m1r Nov 13 '15 at 19:58
  • @4m1r: I don't know, it probably depends on the engine. If there is a performance difference, it's most likely to be negligible. implicit return vs `return;` is probably only useful if you need to return early. writing `return undefined;` might produce different results if `undefined` resolves to a different value. Most (if not all) browsers will throw an error when you attempt to reassign undefined, but in theory, it's possible [here's an example using node.js](https://eval.in/468699) – Elias Van Ootegem Nov 14 '15 at 10:11
  • Thanks @EliasVanOotegem. I think you're right this test appears to confirm that there's negligible difference. https://jsperf.com/implicitreturns/3 – 4m1r Nov 17 '15 at 17:14
  • @EliasVanOotegem does the lack of return cause much performance effect with things like garbage collection on browsers? – Sir Nov 21 '15 at 23:57
  • @Dave: Nope, it doesn't impact it at all. The function will return either way, the scope ends, and local variables can be GC'ed as normal. an empty `return` or an explicit `return undefined;` doesn't change that – Elias Van Ootegem Nov 22 '15 at 16:39
  • but in array.map's callback function, you must return at least a undefined – Sinux Feb 25 '16 at 10:47
  • @Sinux: Like I said, functions _always_ return in JS. If you omit the `return` statement, the function will implicitly return `undefined`, so even without any return statements, the map callback will return `undefined`... – Elias Van Ootegem Feb 25 '16 at 13:48
  • @EliasVanOotegem What do you mean by "it doesn't actually return void" ? – It does. `void` is the same than `undefined` in JS, but before ES5 you could (theoretically) overwrite `undefined`, see https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/void – sebilasse Jun 13 '16 at 10:39
  • 1
    @SebastianLasse: I was referring to `void` in other languages (like C), where you can have functions like `void do_someting(int *arg)`, but you can't have a variable with the type `void`. In C, `void` isn't really a type, so these functions don't _return_ anything, they just jump – Elias Van Ootegem Jun 13 '16 at 12:44
  • Uh, interrupt? I think not. – unwind Aug 30 '17 at 10:39
  • @unwind yeah, I typed that years ago. Changed it to _jump_ now – Elias Van Ootegem Aug 30 '17 at 13:01
  • "If a function really returned nothing at all, there would be no way of knowing what and when to call the next function" This is mistaken. Plenty of languages support functions that do not return any value, but JavaScript happens not to be among them. – Max Barraclough Jul 28 '19 at 15:47
  • @MaxBarraclough: What I meant by that is not really a return value as such, but even functions in C, or pure asm, will end with `leave` and `ret` instructions to jump back to the caller. JS is far less low-level, so the runtime will use the implicit `return` mechanism to jump back to the caller. It's not so much a return value as a return instruction. – Elias Van Ootegem Jul 29 '19 at 09:32
  • @EliasVanOotegem I don't follow. There *is* a value returned: 'undefined'. Also, C permits implicit return for functions with void return type. I don't see that assembly is relevant. – Max Barraclough Jul 30 '19 at 21:12
  • @MaxBarraclough: My point was that, even if there's no return, on a low level (assembly), the function ends with `leave` and `ret`, so technically all functions _return_. In `void f()` funcs in C, nothing will be pushed on the return register (`EAX` for x86). So regardless of language, there is a return instruction, just some languages leave the return value blank (nothing in EAX), whereas JS runtimes translate that to a return value of _undefined_. TL;DR: A return is required, C leaves out `MOV`to `EAX`, JS doesn't – Elias Van Ootegem Jul 31 '19 at 09:53
  • @EliasVanOotegem Is there a way for the caller to know if the called function has explicitly returned `undefined` (via `return undefined`) or implicitly (thus without `return` statement)? – tonix Jan 14 '20 at 07:51
  • @tonix JS is single threaded, so the function has returned once the next statement is executed? Not sure what else you could be asking here – Elias Van Ootegem Jan 14 '20 at 08:12
  • @EliasVanOotegem I would basically need a way to discriminate the case `fn = () => {}; const ret = fn(); // Is "ret" undefined because "fn" returned "undefined" explicitly? In this case no as it's returned implicitly` from this case `fn = () => { return undefined; }; cons ret = fn(); // Is "ret" undefined because "fn" returned it implicitly? In this case, yes`. Something like `if (isRetUndefinedExplicitly(ret, fn)) { ... }` Is it possibile? – tonix Jan 14 '20 at 09:56
  • in your example, the moment you're checking the value of `ret`, the function has returned. It's a single thread – Elias Van Ootegem Jan 14 '20 at 11:11
40

No, return is not necessary.

When no return statement is specified, undefined is returned.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
rhapsodyn
  • 3,272
  • 5
  • 29
  • 36
14

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 can 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:

...

  1. If result.[[Type]] is return, return NormalCompletion(result.[[Value]]).
  2. ReturnIfAbrupt(result).
  3. Return NormalCompletion(undefined).

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

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

mohkhan
  • 11,925
  • 2
  • 24
  • 27