Arrow function without curly braces

前端 未结 7 1079
生来不讨喜
生来不讨喜 2020-11-22 10:52

I\'m new to both ES6 and React and I keep seeing arrow functions. Why is it that some arrow functions use curly braces after the fat arrow and some use parentheses? For exam

相关标签:
7条回答
  • 2020-11-22 11:24

    One can also use curly braces to prevent a single line arrow function from returning a value -- or to make it obvious to the next developer that a single line arrow function shouldn't, in this case, be returning anything.

    For example:

    const myFunc = (stuff) => { someArray.push(stuff) }
    const otherFunc = (stuff) => someArray.push(stuff)
    
    console.log(myFunc())    // --> logs undefined
    console.log(otherFunc()) // --> logs result of push which is new array length
    
    0 讨论(0)
提交回复
热议问题