45

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.

asked Jul 10, 2012 at 4:52
2
  • this inadvisable hack can be used for multiple attributes as well. Commented Apr 19, 2019 at 11:24
  • I use this is c# often enough, it's call a discard Commented Jan 19, 2024 at 16:55

7 Answers 7

55

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 they only needed the second one.

answered Jul 10, 2012 at 4:54

3 Comments

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
In the example you provided, _ is being declared as an argument in the function. He is not using it at all.
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'.
23

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).

answered Jul 10, 2012 at 5:00

Comments

13

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.

answered Apr 18, 2016 at 16:05

Comments

6

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

answered Jul 10, 2012 at 4:56

Comments

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.

answered May 10, 2017 at 11:34

2 Comments

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.
Also, ESLint's no-shadow rule would warn/error about possibly shadowing parent scope variable.
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.

answered Dec 4, 2014 at 15:37

1 Comment

This is possible as well in JavaScript when destructuring arrays. const [a, _, c] = foo(bar);
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/

answered Feb 10, 2022 at 16:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.