Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type is the 'return' keyword?

We use return statements optionally in JavaScript functions. It's a keyword. But what is the actual type of return itself. Actually I got confused, seeing the example:

function add(a, b) {   return (     console.log(a + b),     console.log(arguments)   ); }  add(2, 2); 

Output:

4 [2, 2] 

So, we can pass comma separated expressions into the return statement. Is this a function?

And starting with this, can we take a wild guess that every keyword in JavaScript are ultimately a function?

I've written a small blog as a gist of this discussion. You may want to check it here.

like image 515
Arnab Das Avatar asked Apr 18 '16 09:04

Arnab Das


2 Answers

But what is the actual type of 'return' itself.

It doesn't have a type, it isn't a value.

Attempting typeof return; will give you Unexpected token return.

So, we can pass comma separated expressions into the return statement. Is this a function?

No, while parenthesis can be used to call a function, here they are a grouping operator containing a couple of expressions seperated by a comma operator.

A more useful demonstration would be:

function add(a, b) {   return (     (a + b),     (a - b)   ); }  console.log(add(2, 2)); 

Which outputs 0 because the result of a + b is ignored (it is on the LHS of the comma operator) and a - b is returned.

like image 184
Quentin Avatar answered Sep 24 '22 17:09

Quentin


I'm kinda shocked that no one here has directly referenced the spec:

12.9 The return Statement Syntax ReturnStatement : return ; return [no LineTerminator here] Expression ;

Semantics

An ECMAScript program is considered syntactically incorrect if it contains a return statement that is not within a FunctionBody. A return statement causes a function to cease execution and return a value to the caller. If Expression is omitted, the return value is undefined. Otherwise, the return value is the value of Expression.

A ReturnStatement is evaluated as follows:

If the Expression is not present, return (return, undefined, empty). Let exprRef be the result of evaluating Expression. Return (return, GetValue(exprRef), empty).

So, because of the spec, your example reads:

return ( GetValue(exprRef) )

where exprRef = console.log(a + b), console.log(arguments)

Which according to the spec on the comma operator...

Semantics

The production Expression : Expression , AssignmentExpression is evaluated as follows:

Let lref be the result of evaluating Expression. Call GetValue(lref). Let rref be the result of evaluating AssignmentExpression. Return GetValue(rref). 

...means that every expression will get evaluated until the last item in the comma list, which becomes the assignment expression. So your code return (console.log(a + b) , console.log(arguments)) is going to

1.) print the result of a + b

2.) Nothing is left to execute, so execute the next expression which

3.) prints the arguments, and because console.log() doesn't specify a return statement

4.) Evaluates to undefined

5.) Which is then returned to the caller.

So the correct answer is, return doesn't have a type, it only returns the result of some expression.

For the next question:

So, we can pass comma separated expressions into the return statement. Is this a function?

No. The comma in JavaScript is an operator, defined to allow you to combine multiple expressions into a single line, and is defined by the spec to return the evaluated expression of whatever is last in your list.

You still don't believe me?

<script> alert(foo()); function foo(){     var foo = undefined + undefined;     console.log(foo);     return undefined, console.log(1), 4; } </script> 

Play with that code here and mess with the last value in the list. It will always return the last value in the list, in your case it just happens to be undefined.

For your final question,

And starting with this, can we take a wild guess that every keyword in JavaScript are ultimately a function?

Again, no. Functions have a very specific definition in the language. I won't reprint it here because this answer is already getting extremely long.

like image 26
avgvstvs Avatar answered Sep 21 '22 17:09

avgvstvs