What is the difference between return and return()?

后端 未结 9 1450
误落风尘
误落风尘 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条回答
  •  -上瘾入骨i
    2020-12-13 17:36

    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
    

提交回复
热议问题