Why does the value returned should be on the same line as the return statement in JavaScript?

前端 未结 4 1682
南方客
南方客 2020-12-12 03:16

The following doesn\'t work as I would expect it to:

function test()
{
  // Returns undefined, even though I thought it would return 1
  return
  1;
}
         


        
4条回答
  •  误落风尘
    2020-12-12 03:41

    It's explicitly part of the language spec. If it were not, there would still be return issues:

    if (something())  return
    counter = counter + 1;
    

    Without the semicolon insertion rule, that missing semicolon would trigger behavior that's (I'd argue) just as bizarre as what happens now with return statements split across a newline.

提交回复
热议问题