39

In this post, Nick Craver provided an answer in which he used:

function(_, id)

This code doesn't declare the underscore as a variable before using it. My search on google and on here only points to references to the use of the underscore as a prefix., not as the variable itself. What does it do? I like the solution by Nick but that bit bothers me.

Community
  • 1
  • 1
Siphiwe Gwebu
  • 529
  • 1
  • 5
  • 13

7 Answers7

52

I've seen the underscore used to denote that the variable is a "don't care" variable. It means that it doesn't matter and isn't used at all.

In the case you pointed out, it was used to signify that his function had two arguments, but he only needed the second one.

Ivan
  • 10,052
  • 12
  • 47
  • 78
  • 1
    Thank you Ivan. But do u know why it can be used without being declared? I'd appreciate any reference I can read up more on this – Siphiwe Gwebu Jul 10 '12 at 05:20
  • In the example you provided, `_` is being declared as an argument in the function. He is not using it at all. – Ivan Jul 10 '12 at 05:26
  • 12
    Also, in case someone else was wondering, the reason he wrote `function(_, id)` instead of `function(id)` is because jQuery `attr()` takes a function that has 2 arguments, the element index and the attribute. He didn't need the index; that's why he marked it a 'don't care'. – Bad Request Jun 02 '13 at 04:22
21

Underscore is a valid JS variable name. The parameter named _ in the above example can be used as any other variable.

However, it is usually used to indicate to subsequent (human) reader of the code that whatever passed in will not be used. (The author of the code can be evil/ignorant and use it in the function, though).

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
11

As a beginner, I failed to gather the many aspects associated with this answer, which are scattered over the comments and other answers. So, I'll try to consolidate them below:

Firstly, in the mentioned piece of code, the underscore argument is used as follows:-

$(this).val('').attr('id', function(_, id) { return id + i });

From the jQuery documentation for the attr function here, there exists an overloaded form of attr which is .attr( attributeName, function ). The function in this form is described as

Type: Function( Integer index, String attr )

Hence, it expects two parameters. However, in our code, we need only the id, which happens to be the second parameter.

Now, because of the way JS handles function arguments, we cannot write it as function(id), as JS would map id to index (the first argument expected for function). Thus, the function we write needs to have two parameters.

Here, a standard convention comes into play. As mentioned here,

The underscore character (_) is used as a standard way to indicate an unused function argument.

However, this is only a convention and not a rule. We could name the unused argument as index or unused just as well. That is,

$(this).val('').attr('id', function(unused, id) { return id + i });

would be a valid equivalent.

Thus, such a usage of _ to substitute for an unused argument can be used for any other jQuery function that has a similar overridden form. For example, in this answer, we can see the usage of underscore in the call to $.text(). Just to confirm, $.text() has an overridden form that accepts a function with two arguments, as shown here.

Community
  • 1
  • 1
Sarath Chandra
  • 1,850
  • 19
  • 40
6

By style, _ is typically used as a placeholder variable. A variable which wont be really used in the scope.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
2

Although all answers explain that this is a code style "hack" to indicate an unused parameter it is generally a bad practice(idea). It will override any variable _ declared in a parent scope. This will prevent you to use libraries that define _ (as underscorejs for instance). It is not even some kind of an optimization as it declares a variable anyways.

You should better use a descriptive name of the parameter and consider _ just as a regular variable. Shortening variable names are also considered bad practice. So, if you plan to use this "clever hack", please don't.

venimus
  • 5,907
  • 2
  • 28
  • 38
  • 3
    It's **not** overriding `_` in the parent scope, _it's just shadowing it in the local scope_ and since you're not planning to use that variable in the local scope, there's no risk at all. Though it makes more sense in a small one-liner callback, and if used in a bigger convoluted callback, replacing `_` with a descriptively named parameter is definitely better. – Emile Bergeron Dec 09 '19 at 18:18
  • Also, [ESLint's `no-shadow` rule](https://eslint.org/docs/rules/no-shadow) would warn/error about possibly shadowing parent scope variable. – Emile Bergeron Dec 09 '19 at 18:24
1

As all the other posters before have said, it can be seen as a placeholder, in cases where it is not expected to be used. I have seen something similar in python, when a function can return multiple values, but only the required ones are explicitly defined.

For example

 a, _, c = foo(bar)

In this case the potential return value of 'b' is being ignored (which could still be accessed by _), and 'a' and 'c' are going to be used.

JackDev
  • 4,891
  • 1
  • 39
  • 48
0

I saw this pattern in some legacy code (that was breaking on _ not being defined) and it turned out it was supposed to use this library:

https://underscorejs.org/

Adair
  • 1,697
  • 18
  • 22