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

2

92

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?

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

Can someone please update on this...Thanks.

Cider answered 24/12, 2014 at 12:33 Comment(3)
so @GabyakaG.Petrioli if you call that function with any parameter irrespective of type or no. of parameters..it will call that function?Cider
In the specific function if you pass no parameters it will return lines.x right away, if you pass more than one, then it will use the first only and pass it to lines.x and lines2.x and finally return the chart.Contradistinction
Note that it potentially has meaning in TypeScript, specifically that it's a parameter in a function signature that's not going to be used. (That's a TSLint link, but it seems to have carried over. Haven't found a better "official" source yet.)Kirovograd
C
136

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.

Critic answered 24/12, 2014 at 12:35 Comment(7)
If it was omitted entirely, then he would have nothing to pass to lines.x() and lines2.x(_).Simonetta
To confirm my understanding, passing _ in this case is equivalent to passing null, and _ is used just for aesthetics/readability?Nape
@MarkJackson The _ will still hold a value. You can test this by declaring a function with a parameter _ that logs that parameter, and you will see it logs whatever was passed inBenioff
@MarkJackson: No, _ isn't special; it's a valid identifier in JS, just like x or foo. These definitions are equivalent: a) function f(x) { return x; }, b) function f(_) { return _; }. I'd say @sagar43's answer is incorrect, because _ is in fact used in the body of chart.x, so omitting the parameter (chart.x = function() { ... }) would change the function's semantics.Shifflett
@SimonAlling I've edited the answer to address your concerns.Custard
@Critic Could you please elaborate on how "the second version is safer"?Priam
In your case arr.forEach(function (_, i) {..}), what should I use for the second parameter if I want to get only the third parameter...? 🤔Hovel
K
20

_ 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

Keldah answered 5/1, 2020 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.