What is the difference between return and return()?

Javascript

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

Solution 6 - Javascript

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

Solution 7 - Javascript

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.

Solution 8 - Javascript

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.

Solution 9 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionchris97ongView Question on Stackoverflow
Solution 1 - JavascriptRemcoGerlichView Answer on Stackoverflow
Solution 2 - JavascriptalpartisView Answer on Stackoverflow
Solution 3 - JavascriptZaheer AhmedView Answer on Stackoverflow
Solution 4 - JavascriptEvan KnowlesView Answer on Stackoverflow
Solution 5 - JavascriptSalvador DaliView Answer on Stackoverflow
Solution 6 - JavascriptIQAndreasView Answer on Stackoverflow
Solution 7 - JavascripthijarianView Answer on Stackoverflow
Solution 8 - JavascriptWolfView Answer on Stackoverflow
Solution 9 - JavascriptRahul Singh BhadhoriyaView Answer on Stackoverflow