function a() { return 1; }
function b() { return(1); }
I tested the above code in Chrome\'s console, and both returned 1
.
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
.
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
.
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