-1

I came across the following code:

Array.apply(null, Array(5)).map(function (_, i) {return i;});

What does _ mean in the above code? I tried replacing _ with null but it is saying syntax error. However undefined would work. I am new to Javascript. So any comments would appreciated! Thanks!

geekydude703
  • 187
  • 1
  • 1
  • 13
  • 2
    It's an arbitrary variable name, one that isn't being used in the anonymous function despite being passed, and available, to it. Did you try `console.log(_)` within the function? – David Thomas Feb 04 '18 at 23:32
  • Thanks @MrTux. The other article was very helpful. It's simply a parameter to be ignored in a function. Thanks all! – geekydude703 Feb 04 '18 at 23:37

1 Answers1

1

It is just a regular variable. In some languages, like F#, it means that this argument is not intended to be used. For example, see F#'s underscore: why not just create a variable name?

Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85
  • Yep. `_` is often used as a golfcoding narcissistic way to say *"I know what this argument slot represents but I don't use it in my function body"* - even if does not differs in using `val`, `v`, `key`, `k` etc (the same way `i` *shortcodes* for *index*) which are more bro-I-see-you-friendly – Roko C. Buljan Feb 04 '18 at 23:39