Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of an Underscore in javascript function parameter?

Tags:

javascript

I was going through the code of one of the chart library written in javascript, wherein I've seen passing underscore(_) as a function parameter. What does that mean?

e.g.

chart.x = function(_) {      if (!arguments.length) return lines.x;      lines.x(_);      lines2.x(_);      return chart;    };

Can someone please update on this...Thanks.

like image 547
Azhar Avatar asked Dec 24 '14 12:12

Azhar


People also ask

What is _ in JavaScript function?

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.

What does an underscore before a variable mean JavaScript?

the Underscore Prefix in JavaScript Variables The underscore _ is simply an acceptable character for variable/function names; it has no additional functionality. Underscore variable is the standard for private variables and methods. There is no true class privacy in JavaScript.

What does it mean to underscore an argument?

To underscore is to draw special attention to a fact, idea, or situation. When you're involved in a debate, it's wise to underscore the points that best support your argument. Literally, underscore means “to underline,” or draw a line beneath a word to emphasize it.

Can JavaScript variables start with _?

Naming Conventions for JavaScript VariablesA variable name must start with a letter, underscore ( _ ), or dollar sign ( $ ). A variable name cannot start with a number. A variable name can only contain alpha-numeric characters ( A-z , 0-9 ) and underscores. A variable name cannot contain spaces.


2 Answers

The underscore symbol _ is a valid identifier in JavaScript, and in your example, it is being used as a function parameter.

A single underscore is a convention used by some javascript programmers to indicate to other programmers that they should "ignore this binding/parameter". Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely.

This symbol is often used (by convention again) in conjunction with fat-arrow functions to make them even terser and readable, like this:

const fun = _ => console.log('Hello, World!') fun() 

In this case, the function needs no params to run, so the developer has used the underscore as a convention to indicate this. The same thing could be written like this:

const fun = () => console.log('Hello, World!') fun() 

The difference is that the second version is a function with no parameters, but the first version has a parameter called _ that is ignored. These are different though and the second version is safer, if slightly more verbose (1 extra character).

Also, consider a case like

arr.forEach(function (_, i) {..}) 

Where _ indicates the first parameter is not to be used.

The use of underscores like this can get very confusing when using the popular lodash or underscore libraries.

like image 152
sagar43 Avatar answered Sep 25 '22 08:09

sagar43


_ in fat arrow function is called as throwaway variable. It means that actually we're creating an variable but simply ignoring it. More devs are now a days using this as syntactic sugar or short hand while writing code, as it's easy and one character less to write the code.

Instead of using _, you can use other variables like temp, x, etc

for examples:

() => console.log('Hello World')    _ => console.log('Hello World')     x => console.log('Hello World') 

But personally i prefer to use () type over throwaway variable if no arguments are needed.

See the following code, then you will understand it better.

_ as an argument,

  f = _=> {     return _ + 2 ; } 

f(3) will return 5

For better understanding, check wes bos

like image 27
Anand Raja Avatar answered Sep 21 '22 08:09

Anand Raja