What type is the 'return' keyword? What type is the 'return' keyword? javascript javascript

What type is the 'return' keyword?


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.


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) )

whereexprRef = 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.


Testing what happens when you return parenthesiesed values:

function foo() {    return (1, 2);}console.log(foo());

Gives the answer 2, so it appears that a comma separated list of values evaluates to the last element in the list.

Really, the parenthesis are irrelevant here, they're grouping operations instead of signifying a function call. What's possibly surprising, though, is that the comma is legal here. I found an interesting blog post on how the comma is deal with here:

https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/