3 ms·
That example is particularly confusing because it’s a function definition written as an assignment for IMO no good reason. The outcome is the same either way,
by filoeleven 4y ago
That example is particularly confusing because it’s a function definition written as an assignment for IMO no good reason. The outcome is the same either way, but
function filter(predicate, xs) { return xs.filter(predicate) }
lets you know immediately that “filter” is a function, not a value. There are valid cases where assigning an anonymous function a name via “const” is appropriate, for example when working with higher-order functions, but most of the time it’s not necessary or helpful. I wish it hadn’t caught on.
EDIT: I should have read your comment more carefully. The example function is not a HOF though, it runs xs.filter(pred) when called. A HOF would be
function addConst(x) ( return (y) => x + y }
const add3 = addConst(3)
add3(5) // returns 8
The second line can still be written as
function add3(x) { return addConst(3) }
I think people use the const form because there’s less text to write. Syntax highlighting makes this form more bearable, but I still don’t like it.