4 ms·
It is wrong. "new Array(26)" creates an Array with preallocated space for 26 elements (instead of 26 'undefined' elements, as one might expect), and calling .ma
by coke12 11y ago
It is wrong. "new Array(26)" creates an Array with preallocated space for 26 elements (instead of 26 'undefined' elements, as one might expect), and calling .map() on it will return another empty array. The semantics here are particularly difficult to reason about because map is specified to not execute its callback on undefined elements.
Someone will probably chime in here with a correction, but the point is this -- JS is NOT a friendly language to deal with.
- fcukhn 11y agoActually this code is very stupid and you're half correct. The standard built in Array methods like map or forEach don't work on implicitly defined undefined values but on explicit one they do. For example: [1,2,3,undefined].forEach(ele => console.log(ele)) > 1 > 2 > 3 > undefined On the other hand: [1,2,3,,].forEach(ele => console.log(ele)) > 1 > 2 > 3 So, take the time you dedicate to hating JavaScript to understand the language instead of bitching here you and that stupid jerk above.
- tracker1 11y agoIt will iterate just fine, there are no values (undefined) for each `0` through `length-1` new Array(26).length === 26; //true
- disusered 11y agoSeems like it doesn't, I recently read about this gotcha so I tested it out in JSBin. Using lodash map works as expected, though. JSBin: http://jsbin.com/vasulumeti/1/edit?js,console http://jsbin.com/vasulumeti/1/edit?js,console StackOverflow: http://stackoverflow.com/a/5501711/211291 http://stackoverflow.com/a/5501711/211291