Home » Javascript » What is the difference between return and return()?

What is the difference between return and return()?

Posted by: admin November 30, 2017 Leave a comment

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

Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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.