Search code examples
javascriptlodash

Should I prefer passing the array or the function first as arguments in JavaScript


I came across the filter function in Lodash and I was surprised to see the array as the first argument while the function as the second argument. Shouldn't be the opposite? Is there a reason behind it?

UPDATE:
The few programming languages I know are using the filter() method the opposite way passing the function as the first argument and the array as the second argument.
So I was just curious about the origin of this. Are there other languages or libraries that prefer passing the array/values first and the function after, even in functional programming operations like filtering (I'm not talking about callback functions).
One advantage of passing array/values as the last argument is that I can take easily advantage of Destructuring.
Considerations?
Thanks.


Solution

  • You are referring to using Lodash as a "functional" library (Read more about Lodash FP here) versus Lodash in its normal state. Functional languages, such as Haskell, follow the same methodology as the Lodash FP implementation. The benefits / cons are argued all over the internet, so I will leave that to other discussions.