99
function a() { return 1; }
function b() { return(1); }

I tested the above code in Chrome's console, and both returned 1.

function c() { return "1"; }
function d() { return("1"); }

I also tested the code above, and both of the functions returned "1".

So what is the difference between using return and return()?

10
  • 22
    The answers here will also apply to void(x), typeof(x)
    – Paul S.
    Commented Apr 10, 2014 at 12:55
  • 3
    @PaulS.—was always puzzled by void(0) since it's supposed to be shorthand for undefined. So if it's shorthand, why include unnecessary characters? ;-)
    – RobG
    Commented Apr 10, 2014 at 13:50
  • 1
    @RobG I think it's a carry-over from Python and may actually be useful for special cases where you want to include operators void i = 1; // ReferenceError vs void (i = 1); // undefined
    – Paul S.
    Commented Apr 10, 2014 at 14:30
  • 1
    You might be amused by this question, which is your question applied to C#: stackoverflow.com/questions/2186595/… Commented Apr 10, 2014 at 16:08
  • 3
    possible duplicate of Why use parentheses when returning in Javascript Commented Apr 14, 2014 at 7:07

9 Answers 9

178

The same as between

var i = 1 + 1;

and

var i = (1 + 1);

That is, nothing. The parentheses are allowed because they are allowed in any expression to influence evaluation order, but in your examples they're just superfluous.

return is not a function, but a statement. It is syntactically similar to other simple control flow statements like break and continue that don't use parentheses either.

9
  • 12
    A return statement can be followed by an expression. A return must be in a function, so the function returns the value of the expression, or if there isn't one, undefined.
    – RobG
    Commented Apr 10, 2014 at 13:11
  • 38
    @chris97ong: return is not a function. Commented Apr 10, 2014 at 13:13
  • 34
    If you made return into a function, how would you return a value from it?
    – dan04
    Commented Apr 11, 2014 at 5:42
  • 4
    This is a great answer with a great example, but I think it may be worth adding that 'return' isn't a function into the main answer - considering that it's the root of the misunderstanding that led to this question. Commented Apr 11, 2014 at 9:05
  • 3
    @BiscuitBaker: I added a bit. When I jotted down this short answer I did not expect it to get 50+ upvotes... Commented Apr 11, 2014 at 9:32
22

There is no difference.

return is not a function call, but is a language statement. All you're doing with the parentheses is simply grouping your return value so it can be evaluated. For instance, you could write:

return (x == 0);

In this case, you return the value of the statement x == 0, which will return a boolean true or false depending on the value of x.

2
  • 6
    return is a keyword that signifies the start of a return statement.
    – RobG
    Commented Apr 10, 2014 at 13:14
  • 1
    The evaluation of the return statement needs no "grouping". So in any case there will be a boolean return.
    – Wolf
    Commented Apr 11, 2014 at 9:05
18

Actually here precedence of () is higher so it evaluate first:

Here firstly ("1") get evaluated, in following way:

("1")                     ==> "1"
("1","2")                 ==> "2"
("1","2","3")             ==> "3"
("1"+"2","2"+"2","3"+"2") ==> "32"
(2+3+6)                   ==>  11

so above statement is equivalent to:

return "1";

See visually:

viusal

So there is basically no difference in functionality but second one might be a negligibly bit slow as it firstly solve the brackets.

1
  • 3
    Why would you make a completely unverified assertion that "second one might be a negligibly bit slower" when that is not the case?
    – user663031
    Commented Jun 11, 2015 at 17:07
9

There is absolutely no difference. If you will look at JS (ECMAScript) specification of return statement. Among many other things, it is telling you :

return [no LineTerminator here] Expression ;

that you can provide expression to return. Expression is hello, Math.abs(x), yourCustomFunc(7), or in your second case this can be 1 or (1). Expression 1 after evaluation is the same as (1) and the same as (((((1)))))) or even as something really bizarre like (+(!(+(!1)))).

3
  • 1
    +1 for the official reference. Isn't there a page-by-page reference? - The one-page HTML loads very long.
    – Wolf
    Commented Apr 11, 2014 at 9:13
  • @Wolf I have not found such. Here is another one, but it is also one page people.mozilla.org/~jorendorff/… Commented Apr 11, 2014 at 9:30
  • Yes, sad. I'd also no success. What about expanding your quote to the whole syntax of the return statement?
    – Wolf
    Commented Apr 11, 2014 at 9:33
9

There is no difference, the parenthesis are optional. See MSDN:

return[(][expression][)];

The optional expression argument is the value to be returned from the function. If omitted, the function does not return a value.

You use the return statement to stop execution of a function and return the value of expression. If expression is omitted, or no return statement is executed from within the function, the expression that called the current function is assigned the value undefined.

6
  • 8
    Only MS would think to make grouping "optional" in a return statement. They are as "optional" there as in any expression, or many other parts of the grammar.
    – RobG
    Commented Apr 10, 2014 at 13:18
  • @RobG: But apparently, they're far more likely to be asked about in a return statement. :P MS probably got tired of fielding the question from JS noobs.
    – cHao
    Commented Apr 10, 2014 at 13:38
  • 2
    The M$ explanation is incorrect. The (), is not part of the return statement. Commented Apr 12, 2014 at 13:09
  • 4
    "There is a difference for the reader." Well, yes: The MSDN spec erroneously implies that return (expression and return expression) are legal.
    – Jim Balter
    Commented Apr 13, 2014 at 4:16
  • 2
    @JimBalter Along with return (), return ( and return ) :P
    – Paul
    Commented Apr 16, 2014 at 0:31
9

return is a statement a keyword that starts the return statement, not a function.

As has been mentioned, the extra parentheses affect evaluation order, but are not used to "execute" the function named return. That is why these lines work without any problems:

return (1);
var a = (1);

They are, in effect, identical to these lines:

return 1;
var a = 1;

The reason return() throws a syntax error is for the exact reason the following line throws an error (return statement included for comparison):

return();    // SyntaxError: syntax error
var a = ();  // SyntaxError: syntax error
2
  • And just as a side note, if return doesn't include anything after it, it assumes you wanted to return undefined.
    – IQAndreas
    Commented Apr 11, 2014 at 5:40
  • @Wolf The expressions is optional, so return by itself is a return statement.
    – Paul
    Commented Apr 16, 2014 at 0:33
5

There is huge difference for humans, and zero difference for Javascript engine.

return 1 is a statement declaring that we need to immediately exit the function yielding value of 1.

return(1) is the same statement disguised as the function call by the idiotic convention that you are not obliged to insert space outside of parentheses in Javascript. If you would use code like this in production system, any maintainer will come to your office with stake and torches, after spending some time trying to decide whether you do really have return() function somewhere in codebase or just don't know what return keyword is for.

As many other people have already correctly said, parentheses do nothing except "group" with higher precedence the literal for the number 1.

4
  • 4
    "There is huge difference for humans" -- Not competent ones. " the same statement disguised as the function call" -- there is no return function. "the idiotic convention that you are not obliged to insert space outside of parentheses in Javascript" -- almost no language requires that; calling this idiotic is idiotic. "any maintainer will come to your office with stake and torches" -- no competent one would. return(1) is poor practice but people who get upset about such things are wasting resources and creating a bad atmosphere.
    – Jim Balter
    Commented Apr 13, 2014 at 4:27
  • @JimBalter "Not competent ones" -- ha! "Almost no language requires that" -- that's why I said "convention". If it's idiotic, it's called "idiotic". "There is no return function" -- the fact that interpreter will forbid you from registering a function with name return will not help the reader of the code not to question his sanity first, so, this code is waste. return(1) is not just "poor practice", is a dangerous sign of someone not knowing what he's doing!
    – hijarian
    Commented Apr 14, 2014 at 14:20
  • 2
    So you either don't know what "obliged" means, or you're changing your claim ... either way I won't waste any more of my time on you.
    – Jim Balter
    Commented Apr 15, 2014 at 0:24
  • I doubt the troth of your last sentence; return has (in all languages I know that have this keyword) least precedence. So, if the optional expression is present, it is always evaluated first.
    – Wolf
    Commented May 4, 2020 at 9:16
4

In the return statement, the parentheses around the expression are already built in.

In JavaScript, as in many other languages (like C, C++, Java, Python), the return statement has two parts: the keyword return and an (optional) expression. So in, any case, all that is following the return keyword is first evaluated as an expression, after that, the return statement is "executed" by passing the control back to the caller.

To use or not to use parentheses is a matter of style, whereas most style guides forbid them for trivial cases like the one quoted in your question, because it makes return falsely looking like a function.

Later addendum

If with parentheses or without, never forget to place the optional expression behind the return, that is, in the same line. The real pitfall with return in JavaScript lies in adding a line break after it:

function test() {
  return 
  1;
}

... because above test function will return undefined.

2

by adding parenthesis, we have made sure that javascript doesn't insert a semicolon before multiple statements written after return, for reference:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion

example:

return a + b;

is transformed to

return; a + b;

by ASI.

The console will warn "unreachable code after return statement". To avoid this problem (to prevent ASI), you could use parentheses:

return ( a + b );
code copied from:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

1
  • This is absolutely correct and that way it makes sense for complex expressions. But you should add on the same line or directly behind return in the first sentence to make it even more clear. I think that the downvote comes from misinterpretation of this.
    – Wolf
    Commented May 4, 2020 at 9:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.