Javascript: what does function(_) mean

tldr picture tldr · May 28, 2014 · Viewed 7.6k times · Source

I'm going through the bacon.js slide at: http://raimohanska.github.io/bacon.js-slides/1.html

In the 1st line of the 2nd block, it says:

function always(value) { return function(_) { return value } }

what does function(_) mean?

Answer

user2864740 picture user2864740 · May 28, 2014

In this case _ is just a function parameter - a single underscore is a convention used by some programmers to indicate "ignore this binding/parameter".

Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely. Such a "throw-away" identifier is found more commonly in other languages, but consider a case like arr.forEach(function (_, i) {..}) where _ indicates the first parameter is not to be used.