Personally, I favor function declarations. However, if the logic is simple enough, I'll consider using a function expression using an arrow function (typically for functional composition).
Both of these are functionally equivalent:
const isTruthy = (item) => item function isTruthyLong (item) { return item }
When you use them as the callback for array.filter() they behave the same.
array.filter()
let a = Array(3) // [undefined, undefined, undefined] a[1] = 'Hi mom!' let short = a.filter(isTruthy) let long = a.filter(isTruthyLong) console.log(a) // [undefined, 'Hi mom!', undefined] console.log(short) // ['Hi mom!'] console.log(long) // ['Hi mom!']
CodePen Demo
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Personally, I favor function declarations. However, if the logic is simple enough, I'll consider using a function expression using an arrow function (typically for functional composition).
Both of these are functionally equivalent:
When you use them as the callback for
array.filter()
they behave the same.CodePen Demo