2 ms·
> [,,,].length // -> 3 I don't think this is too weird if you think about it. JS allows trailing commas, so the last one is ignored. Effectively this is `[ und
by Sebb767 5y ago
> [,,,].length // -> 3
I don't think this is too weird if you think about it. JS allows trailing commas, so the last one is ignored. Effectively this is `[ undefined, undefined, undefined, ]`. A syntax error would have made sense here, but the length of three is a result of the usual syntax rules, not a particular strange quirk of JS.
- SCLeo 5y agoSorry to be pedantic, but `[,,,]` creates holes instead of undefined. They are different, because for example `[,,,].forEach(() => console.info(1))` doesn't do anything, but `[undefined,undefined,undefined,].forEach(() => console.info(1))` prints three "1"'s.