-3

Apparently return can be invoked as a function?

function say() {
   return (
     console.log("Am"),
     console.log("I"),
     console.log("a"),
     console.log("function?")
  );
}

say();

Does this mean that return is a function?

6
  • no - it is still happening directly inside the stack of say, not in some new function. Commented Dec 8, 2014 at 19:08
  • No, it is a keyword. Just like ++ isn't a function but you can ++(x) Commented Dec 8, 2014 at 19:09
  • 3
    this is the syntax of return. , is not ; this is an expression not a statement. Commented Dec 8, 2014 at 19:09
  • Related question: Why use parentheses when returning in Javascript Commented Dec 8, 2014 at 19:09
  • you can put anything between parenthesis. (a = 2). a = (2) (a) = 32 Commented Dec 8, 2014 at 19:12

1 Answer 1

3

The keyword "return" is not a function. The documentations says that return is a statement.

The return statement ends function execution and specifies a value to be returned to the function caller.

That means

(
     console.log("Am"),
     console.log("I"),
     console.log("a"),
     console.log("function?")

)

is evaluated as an expression and the result of this expression is returned from the say() function. The parentheses only group the four calls to console.log together. The commas serve to separate the function calls within the expression.

The this expression will return the value returned from the last element in the list, which in this case is console.log("function?"). And the function console.log returns undefined.

So the function say() would return undefined.

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

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.