What is the difference between return and return()?

后端 未结 9 1421
误落风尘
误落风尘 2020-12-13 16:37
function a() { return 1; }
function b() { return(1); }

I tested the above code in Chrome\'s console, and both returned 1.



        
相关标签:
9条回答
  • 2020-12-13 17:13

    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.

    0 讨论(0)
  • 2020-12-13 17:23

    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.

    0 讨论(0)
  • 2020-12-13 17:25

    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.

    0 讨论(0)
  • 2020-12-13 17:25

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

    0 讨论(0)
  • 2020-12-13 17:27

    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

    0 讨论(0)
  • 2020-12-13 17:31

    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.

    0 讨论(0)
提交回复
热议问题