Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JavaScript functions always return a value?

Tags:

javascript

I'm taking a course in JavaScript programming, and the instructor said that a typical JavaScript function always returns a value. Even when we don't provide any explicit return value, the engines return undefined.

Is that true? If so, why?

like image 675
Luís Assunção Avatar asked Jan 04 '14 00:01

Luís Assunção


People also ask

Do JavaScript functions always return values?

A function without a return statement will return a default value. In the case of a constructor called with the new keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined .

Why do functions return a value?

A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.

Why a function in JavaScript is a value?

In JavaScript, however, functions are not only syntax but also values, which means they can be assigned to variables, stored in the properties of objects or the elements of arrays, passed as arguments to functions, and so on.

Does a function always need to return a value?

NO, a function does not always have to have an explicit return statement. If the function doesn't need to provide any results to the calling point, then the return is not needed. However, there will be a value of None which is implicitly returned by Python.


2 Answers

It's true—because that's how JavaScript was designed.

But I don't think that's the answer you were looking for, so let's think about it...
Try to put yourself in the shoes of Brendan Eich, the person who designed JavaScript!

In static languages, there is usually a distinction between a function that doesn't return anything (void function), and a function that returns some value. Brendan chose to design a dynamic language, that is, a language that doesn't require you to define function return types. So JavaScript doesn't check what you return from the function, giving you full freedom.

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

function computeSomething() {   return 2; } 

... or a 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 don't need to compute anything—you only need to do something.
So you don't return anything.

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

We may, however, want to exit a function in the middle of it, and since return <value> already does exactly that, it makes sense to allow writing return without a 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 to ensure JavaScript adoption.

Aye, there's the rub: what happens if we put a plain return into a function supposed to compute something? Note that we can't outlaw this: one of our earlier decisions was to make JavaScript a dynamic language, where we don't 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 could have decided to raise an error in this case, but he wisely decided not to, because it would lead to hard-to-find errors and too easily breakable code.

So an empty return got a meaning “return undefined”.

But what's the difference between the function returning early, or at its end? There shouldn't be any, from the calling code's point of view. Calling code is not supposed to know when exactly the function returned; it is only interested in return value (if any).

The only logical conclusion thus would be to make undefined the “default” return value if function does not specify one via explicit return <value> operator. Thus, return and function-executed-to-its-end semantics match.

Python, another dynamic language that came before JavaScript, solves this problem in the same way: None is returned if function doesn't specify return value.

like image 77
Dan Abramov Avatar answered Oct 20 '22 19:10

Dan Abramov


That's by ECMAScript specification

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

Essentially any JS function is compiled as if it has implicit return undefined; at the end:

function foo() {   ...    return undefined; // implicit non-visible statement   } 
like image 31
c-smile Avatar answered Oct 20 '22 19:10

c-smile