Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between return and return()?

Tags:

javascript

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

like image 535
chris97ong Avatar asked Apr 10 '14 12:04

chris97ong


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

like image 114
RemcoGerlich Avatar answered Sep 21 '22 13:09

RemcoGerlich


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.

like image 36
alpartis Avatar answered Sep 19 '22 13:09

alpartis