10

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?

tldr
  • 10,743
  • 11
  • 69
  • 111
  • `_` is simply a parameter in this context. – Sam P May 28 '14 at 05:08
  • 1
    It’s not meaningful here, in JavaScript, but the convention is that `_` represents an ignored value. – Ry- May 28 '14 at 05:12
  • @false Ironically "ignore this" did not work, and brought OP attention. I also have never heard about this convention. Interesting. – dfsq May 28 '14 at 05:14

3 Answers3

22

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.

user2864740
  • 54,112
  • 10
  • 112
  • 187
  • That makes at least some sense of it. Thanks for sharing. – Oleg Gryb May 28 '14 at 05:33
  • 1
    F# (and similar) do this consistently during pattern matching. C# can also do this for lambda variables. Its definitely common.. however useless it may be in the code you are reviewing. – Simon Whitehead May 28 '14 at 12:29
  • 4
    I used this convention along with the Haskell type signatures in the slides. I prefer typesafe languages and tend to use some Haskell conventions in Javascript code too. Sorry for causing confusion! – raimohanska Jun 02 '14 at 12:16
  • but if it is not going to be used, why not just say function (i)={...}? is not the same? – Victor Apr 24 '20 at 23:25
  • 1
    @Victor It's a *convention*. The parameter might as as well be called `unused` or `thisParameterIsNotUsedUsed`. – user2864740 Apr 24 '20 at 23:47
  • yes I understand that it a convention for saying we are not using it, I just did not understand that sometimes we only need the iterator or index value (key) and in that case the callback function will require 2 parameters, the current value, and the index. – Victor Apr 26 '20 at 16:24
12

It's an anonymous function with one argument, the name of that argument is _.

I don't know why they bother with the argument, since the function doesn't use it.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Upvoted to reverse the downvote, as I'm pretty sure that this answer is correct. The underscore becomes the name of the first argument. It is not used. – Zach Lysobey May 28 '14 at 05:11
  • "Upvoted to reverse the downvote" - that's funny, but you're right there are many nervous people around :) – Oleg Gryb May 28 '14 at 05:15
  • +1 for `I don't know why they bother with the argument, since the function doesn't use it.` – jhyap May 28 '14 at 05:16
1

It's the same as putting any other identifier to a list of arguments according to this document: http://mathiasbynens.be/notes/javascript-identifiers

You'll find in this doc that _ is a legal character that an identifier can start with.

There is no any meaning for this in your example, probably the author just thought that it's cooler than just ().

Oleg Gryb
  • 4,361
  • 1
  • 22
  • 36