Hacker News new | ask | show | jobs
by dspillett 2515 days ago
Seems perfectly logical to me: an array of length 5 with four populated elements and one empty one (3). Though I did double-check my understanding that the length property would report 5 rather than 4 (it does).

What looks out of place to you in that example?

Would it make more sense to you with a very slightly less arbitrary example, perhaps arr = ['Value for 0', 'Value for 1', , 'Value for 3', 'Value for 4']; instead of simple mapping ints to ints?

Because array contents are mutable [even if the array variable itself is declared const] that third index may be populated at a later point in the code.

1 comments

> What looks out of place to you in that example?

Most languages don't have sparse arrays so it's really weird.

> Would it make more sense to you with a very slightly less arbitrary example, perhaps arr = ['Value for 0', 'Value for 1', , 'Value for 3', 'Value for 4']; instead of simple mapping ints to ints?

You'd usually put an explicit `null` there, especially as HOFs skip "empty" array cells so

    ['Value for 0', 'Value for 1', , 'Value for 3', 'Value for 4'].map(_=>1)
returns

    [1, 1, , 1, 1]
which is rarely expected or desirable.