Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore as a JavaScript variable?

Tags:

javascript

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.

like image 510
Siphiwe Gwebu Avatar asked Jul 10 '12 04:07

Siphiwe Gwebu


People also ask

Can you use underscores in JavaScript variables?

Naming variables Variable names are pretty flexible as long as you follow a few rules: Start them with a letter, underscore _, or dollar sign $. After the first letter, you can use numbers, as well as letters, underscores, or dollar signs. Don't use any of JavaScript's reserved keywords.

What is the use of _ in JavaScript?

The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

What is _ each in JavaScript?

The _.each function accepts an array or an object, an iteratee function and an optional context object, the iteratee function is invoked once and in order for each array item The iteratee function provides 3 arguments item - The current iterated object (or value if an object was passed) i - The index of the iterated ...

Should JavaScript variables be camelCase or underscore?

A global JavaScript variable is written in camelCase if it is mutable. A global JavaScript variable is written in UPPERCASE if it is immutable.


5 Answers

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.

like image 73
Ivan Avatar answered Sep 30 '22 20:09

Ivan


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

like image 28
nhahtdh Avatar answered Sep 30 '22 19:09

nhahtdh


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.

like image 23
Sarath Chandra Avatar answered Sep 30 '22 20:09

Sarath Chandra


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

like image 25
UltraInstinct Avatar answered Sep 30 '22 21:09

UltraInstinct


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.

like image 20
venimus Avatar answered Sep 30 '22 19:09

venimus