Search code examples
javascriptecmascript-6arrow-functionscode-readability

is it ok to use succinct arrow function syntax if I'm not returning a value?


Let's say I have the function pageScroller that doesn't return any value. Given that the succinct ES6 arrow function syntax implicitly returns the value, is it correct still ok to use it without block around?

const goToPage = pageNumber => pageScroller(pageNumber)

or I should always prefer the explicit block instead? I.e.:

const goToPage = pageNumber => {
  pageScroller(pageNumber)
}

My only worrying is that people reading the code can expect that pageScroller returns a value. What do you think?


Solution

  • This is largely opinion based but you're not wrong to want to be cautious about other people thinking you might be trying to assign a value.

    I'd recommend looking at the code again after a couple weeks of working with something else and see if you recognize what it's doing immediately. Also, think about if the goToPage const will ever need to do anything other than call the pageScroller. If there's any chance it would, you might be better off calling the explicit block. If there isn't, maybe see if you can just call pageScroller directly instead of using a const?