When should I use `return` in es6 Arrow Functions?

后端 未结 5 2217
后悔当初
后悔当初 2020-11-21 05:40

The new es6 arrow functions say return is implicit under some circumstances:

The expression is also the implicit return value of that fun

5条回答
  •  无人共我
    2020-11-21 05:59

    Arrow functions allow you to have an implicit return: values are returned without having to use the return keyword.

    It works when there is a on-line statement in the function body:

    const myFunction = () => 'test'
    
    console.log(myFunction()) //'test'

    Another example, returning an object (remember to wrap the curly brackets in parentheses to avoid it being considered the wrapping function body brackets):

    const myFunction = () => ({value: 'test'})
    
    console.log(myFunction()) //{value: 'test'}

提交回复
热议问题