var arr = ["string", , ]
console.log(arr.length)
Why is the length of arr 2 and not 3?
var arr = ["string", , ]
console.log(arr.length)
Why is the length of arr 2 and not 3?
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?
section 11.1.4