Why is `throw` invalid in an ES6 arrow function?

后端 未结 3 1539
天涯浪人
天涯浪人 2020-12-01 21:00

I\'m just looking for a reason as to why this is invalid:

() => throw 42;

I know I can get around it via:

() =&         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 21:09

    If you omit the braces in an arrow function, you create an implicit return, which is equivalent to creating an explicit return with the braces, like so: () => { return throw 42 };

    However, you can only return expressions, not statements. And throw is a statement.

提交回复
热议问题