4
var arr = ["string", , ]
console.log(arr.length)

Why is the length of arr 2 and not 3?

Jeremy W
  • 1,889
  • 6
  • 29
  • 37
not satan
  • 142
  • 8

1 Answers1

1

This is a feature of array literals introduced back in ES3.

You can put leading or trilling commas as "empty" elements

so [,,,,1,2,3,,] would yield an array of length 7, with first 4 and the last elements empty.

If you put this expression into the console in Firefox 34.0.5 it would say

Array [ <4 empty slots>, 1, 2, 3, <1 empty slot> ]

Sources:

Are trailing commas in arrays and objects part of the spec?

http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf

section 11.1.4

Community
  • 1
  • 1
XrXr
  • 2,027
  • 1
  • 14
  • 20