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?

5
  • 1
    Well, there's no return value. How would you call a return value that has not been defined? :) Commented Jun 27, 2018 at 9:35
  • 1
    Yes it is. ... ... Commented Jun 27, 2018 at 9:35
  • Every expression results in a value, otherwise it would be a statement. A function call is an expression. The value for "no value" is undefined. Commented Jun 27, 2018 at 9:37
  • @deceze what is statement and expression Commented Jun 27, 2018 at 9:42
  • A statement is something like for (...); you can only use it by itself, it does not result in a value. An expression is something that results in a value and can be used as part of a larger expression or statement. Commented Jun 27, 2018 at 9:44

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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:

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.