What is the difference between return and return()?
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 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))))
.
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:
So there is basically no difference in functionality but second one might be a negligibly bit slow as it firstly solve the brackets.
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.