What is the difference between return and return()?

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

提交回复
热议问题