3
var f1 = () => {
    return undefined;
};

var f2 = () => {
};

var a = f1(); // undefined
var b = f2(); // undefined

I know the results but I want to know more in-depth. Is that mean in Javascript functions, no return statement identical to return undefined?

skitty jelly
  • 375
  • 1
  • 10

3 Answers3

5

I know the results but I want to know more in-depth. Is that mean in Javascript functions, no return statement identical to return undefined?

It is in effect, yes. The specification differentiates between the two, but in pragmatic terms, calling a function that "falls off the end" vs. return; vs. return undefined; all have exactly the same end result in terms of what have the call results in: undefined.

In my answer to the dupetarget (I should have realized!) I explain how the spec differentiates them, but again, it's just a spec distinction, not something you can observe in actual code.

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
3

It depends.

For functions who are not used as an instance the default return value is undefined.

For a constructor, called with new, it returns this object as default.

Sources:

Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
1

If there is no return the return value will be undefined it's basically the same as doing return; (without value) since the function will "return" when it is finised (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return for information to return)

If you initialize a function (more like a class) than the returned value will be the instance of this function instead.

Lars-Olof Kreim
  • 280
  • 1
  • 8