Underscore in javascript function parameter

I was looking at the code of one of the chart libraries written in javascript, in which I saw the underscore (_) pass as a parameter to the function. What does it mean?

eg.

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

Can someone please update this ... Thanks.

+5
source share
2 answers

In this case, _ is just a parameter of the function - one underscore is the convention used by some programmers to indicate "ignore this binding / parameter parameter".

Since JavaScript does not perform parameter checking, the parameter may be completely omitted. This "drop" identifier is most often found in other languages, but consider a case like arr.forEach (function (_, i) {..}), where _ indicates that the first parameter should not be used.

+10
source

_ is a valid variable name in JavaScript.

You can learn more about valid JavaScript identifiers by reading this article: https://mathiasbynens.be/notes/javascript-identifiers

-1
source

Source: https://habr.com/ru/post/1209836/


All Articles