What is the difference between return and return()?

后端 未结 9 1447
误落风尘
误落风尘 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:31

    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.

提交回复
热议问题