Javascript Array Problem

arraysjavascript

Why is JavaScript returning the wrong array length?

var myarray = ['0','1'];
delete myarray[0];
alert(myarray.length); //gives you 2

Best Answer

The "delete" doesn't modify the array, but the elements in the array:

 # x = [0,1];
 # delete x[0]
 # x
 [undefined, 1]

What you need is array.splice