0

I saw the explanation of Array.from() on MDN, and at the bottom there is a code block like this below:

const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));

I did not understand why parameter can be like (_, i), can somebody explain that to me?

  • 5
    This is an indication that the first arg (which is the element at index `i`) in that code it's irrelevant, useless, Etc., because the author wants to use the index `i` only. – Ele Aug 30 '20 at 11:17
  • 2
    Technically it stores the value in argument `_` and the index in argument `i`. But the value is never used. It's convention to call such unused arguments `_`. But you could also use any other name, it's just a variable name. – CherryDT Aug 30 '20 at 11:22

1 Answers1

2

In the JavaScript developer ecosystem, it's a coding-convention to use a singular underscore _ character to denote a "discarded" or "irrelevant" parameter or value that must still be specified.

It is not a part of the JavaScript language, however. (So you can still use _ as a variable identifier).

Some languages, like C# 7, do give _ special-treatment - however.

Dai
  • 141,631
  • 28
  • 261
  • 374