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

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
chris97ong
  • 6,870
  • 7
  • 32
  • 52
  • 22
    The answers here will also apply to `void(x)`, `typeof(x)` – Paul S. Apr 10 '14 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 Apr 10 '14 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. Apr 10 '14 at 14:30
  • 1
    You might be amused by this question, which is your question applied to C#: http://stackoverflow.com/questions/2186595/is-there-a-difference-between-return-myvar-vs-return-myvar/2187498#2187498 – Eric Lippert Apr 10 '14 at 16:08
  • @PaulS. WTF? Python doesn't have a `void` so how can it be a "carry-over"? In fact in python the expression `identifier expr` is *always* a `SyntaxError` (where `expr` doesn't have outer parenthesis). – Bakuriu Apr 10 '14 at 18:56
  • 1
    @Bakuriu I was thinking of `print(foo)` – Paul S. Apr 10 '14 at 19:03
  • @PaulS. In PHP, I don't like the `print $foo` syntax, but I like the `echo $foo` syntax. I just feel it should be `print($foo)` rather than `print $foo`. I feel like it's a function, whereas `echo $foo` is a command; reminiscent of my DOS days. – Cole Tobin Apr 11 '14 at 19:06
  • @Alma Do: Since there are no "thanks" answers etc, there is no reason to protect this question. – Felix Kling Apr 14 '14 at 06:30
  • 3
    possible duplicate of [Why use parentheses when returning in Javascript](http://stackoverflow.com/questions/20824558/why-use-parentheses-when-returning-in-javascript) – Qantas 94 Heavy Apr 14 '14 at 07:07
  • Why not post a question asking what the difference is between `a` and `(a)`. Maybe you get 94 upvotes on that one too. –  Jun 11 '15 at 17:04

9 Answers9

179

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.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • Then how does `return` work? In any other function, you need a pair of parentheses to indicate the arguments. Why do you not need the parentheses to indicate the return value in `return`? – chris97ong Apr 10 '14 at 12:58
  • 12
    A [*return statement*](http://ecma-international.org/ecma-262/5.1/#sec-12.9) 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 Apr 10 '14 at 13:11
  • 38
    @chris97ong: return is not a function. – RemcoGerlich Apr 10 '14 at 13:13
  • @RemcoGerlich Who says we can't make it one ;) – Cole Tobin Apr 11 '14 at 00:41
  • 34
    If you made `return` into a function, how would you return a value from it? – dan04 Apr 11 '14 at 05: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. – BiscuitBaker Apr 11 '14 at 09:05
  • 3
    @BiscuitBaker: I added a bit. When I jotted down this short answer I did *not* expect it to get 50+ upvotes... – RemcoGerlich Apr 11 '14 at 09:32
  • 2
    @dan04 `window['return'] = function(x) {return x;}`. `window['return'](123)` returns 123 :) – YingYang Apr 11 '14 at 12:34
  • 1
    "Who says we can't make it one" -- logic. – Jim Balter Apr 13 '14 at 04:12
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.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
alpartis
  • 1,086
  • 14
  • 29
  • 6
    [*return*](http://ecma-international.org/ecma-262/5.1/#sec-7.6.1.1) is a keyword that signifies the start of a [*return statement*](http://ecma-international.org/ecma-262/5.1/#sec-12.9). – RobG Apr 10 '14 at 13:14
  • 1
    The evaluation of the return statement needs no "grouping". So in any case there will be a boolean return. – Wolf Apr 11 '14 at 09:05
19

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.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • 2
    Why would you make a completely unverified assertion that "second one might be a negligibly bit slower" when that is not the case? –  Jun 11 '15 at 17:07
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.

Community
  • 1
  • 1
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
  • 8
    Only MS would think to make grouping "optional" in a [*return statement*](http://ecma-international.org/ecma-262/5.1/#sec-12.9). They are as "optional" there as in any expression, or many other parts of the grammar. – RobG Apr 10 '14 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 Apr 10 '14 at 13:38
  • There *is a difference* for the reader. And the odd idea that return could be a function arises from using parentheses. – Wolf Apr 11 '14 at 09:07
  • 2
    The M$ explanation is incorrect. The `()`, is not part of the return statement. – Salman A Apr 12 '14 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 Apr 13 '14 at 04:16
  • 2
    @JimBalter Along with `return ()`, `return (` and `return )` :P – Paul Apr 16 '14 at 00:31
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)))).

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 1
    +1 for the official reference. Isn't there a page-by-page reference? - The one-page HTML loads very long. – Wolf Apr 11 '14 at 09:13
  • @Wolf I have not found such. Here is another one, but it is also one page https://people.mozilla.org/~jorendorff/es6-draft.html#sec-return-statement – Salvador Dali Apr 11 '14 at 09:30
  • Yes, sad. I'd also no success. What about expanding your quote to the whole syntax of the return statement? – Wolf Apr 11 '14 at 09:33
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
Community
  • 1
  • 1
IQAndreas
  • 8,060
  • 8
  • 39
  • 74
  • And just as a side note, if `return` doesn't include anything after it, it assumes you wanted to return `undefined`. – IQAndreas Apr 11 '14 at 05:40
  • @Wolf The expressions is optional, so `return` by itself is a return statement. – Paul Apr 16 '14 at 00: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.

Wolf
  • 9,679
  • 7
  • 62
  • 108
hijarian
  • 2,159
  • 1
  • 28
  • 34
  • 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 Apr 13 '14 at 04: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 Apr 14 '14 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 Apr 15 '14 at 00: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 May 04 '20 at 09: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.

Wolf
  • 9,679
  • 7
  • 62
  • 108
1

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

  • 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 May 04 '20 at 09:34