Why do JavaScript functions always return a value?

I am studying a JavaScript programming course, and the instructor said that a typical JavaScript function always returns a value. Even when we do not provide any explicit return value, engines return undefined .

It's true? If so, why?

+48
javascript
Jan 04
source share
4 answers

This is true because that is how JavaScript was designed.

But I don’t think the answer you were looking for, so think about it ...
Try putting yourself in the shoes of Brendan Eich , the person who developed JavaScript!

In static languages, there is usually a difference between a function that returns nothing ( void ) and a function that returns some value. Brendan chose a dynamic language, that is, a language that does not require determining the types of returned functions. Therefore, JavaScript does not check what you return from a function , giving you complete freedom.

You may have a function that returns a number ...

 function computeSomething() { return 2; } 

... or string ...

 function computeSomething() { return 'hi'; } 

... or, in fact, any of them:

 function computeSomething() { if (Math.random() > 0.5) { return 2; } else { return 'hello'; } } 

Sometimes you do not need to calculate anything - you just need to do it .
Therefore, you are not returning anything.

 function doSomething() { console.log('doing something'); } 

We may, however, want to exit the function in the middle of it, and since return <value> already doing just that, it makes sense to allow return without value to support this use case:

 function doSomething(num) { if (num === 42) { return; } while (true) { doSomethingElse(); } } 

This is also consistent with C / Java syntax, which was one of the goals of JavaScript adoption.

Yes, there rub: what happens if we put a simple return in a function that needs to calculate something? Please note that we cannot forbid this : one of our previous decisions was to make JavaScript a dynamic language, where we do not check what the function returns.

 function computeSomething(num) { if (num === 42) { return; // just return? o_O } if (Math.random() > 0.5) { return 2; } else { return 'hello'; } } var x = computeSomething(2); // might be 2, might be 'hello' var y = computeSomething(42); // ??? 

Of course, Brendan might have decided to make a mistake in this case, but he wisely decided not to, because it would lead to hard-to-reach errors and too easily destructible code.

Thus, an empty return got the value "return undefined ".

But what's the difference between a function returning early or late? They should not be, from a code point of view. The call code does not need to know exactly when the return function is; he is only interested in the return value (if any).

The only logical conclusion, therefore, would be to make undefined default return value if the function does not specify one through the explicit return <value> operator. Thus, the correspondence of return and the semantics performed by functions, and.

Python, another dynamic language that came before JavaScript, solves this problem the same way: None returned if the function does not specify a return value .

+80
Jan 04 '14 at 1:06
source share
— -

This is an ECMAScript specification.

13.2.1 [[Call]] ... 6. Otherwise, result.type should be normal. Return undefined.

Essentially, any JS function compiles as if it had an implicit return undefined; in the end:

 function foo() { ... return undefined; // implicit non-visible statement } 
+11
Jan 04 '14 at 1:06
source share

Even when we do not provide any explicit return value, the engines return "undefined". It's true?

Not really. As far as I understand, a function that returns nothing ... returns nothing. However, if you assign a variable to the result of such a function call, the expression will be evaluated as undefined .

EDIT

I stand fixed. Here is the relevant part of the specification:




13.2.1 [[Call]]

When the [[Call]] internal method is called for an object of function F with this value and a list of arguments, the following steps are performed:

  • Let funcCtx be the result of creating a new execution context for the function code using the value of the internal property F [[FormalParameters]], the arguments passed to List args and this value, as described in 10.4.3.
  • Let the result be the result of evaluating FunctionBody, which is the value of the internal property F [[Code]]. If F does not have an internal [[Code]] property or if its value is an empty FunctionBody, then the result will be (normal, undefined, empty).
  • Close the funcCtx execution context by restoring the previous execution context.
  • If result.type is throw, then throw result.value.
  • If result.type is return, then return result.value.
  • Otherwise, result.type should be normal. Return undefined.
+5
Jan 04 '14 at 1:00 a.m.
source share

In javascript, functions are objects. Therefore, you can always assign a function to a variable, as if you had assigned some other object, and the interpreter will force you to determine the value (which may be undefined.)

+2
Jan 04 '14 at 1:03
source share



All Articles