Jump to content

JavaScript Array.length


centenial

Recommended Posts

Hi,

 

Can anyone explain to me the behavior of this code? I don't understand why JavaScript's Array.length property isn't updated after I delete an element from that array.

 

var arr = new Array();
arr[0] = 'test';
alert(arr.length); // 1
delete arr[0];
alert(arr.length);  // should be 0, but javascript returns 1

 

Am I doing something wrong, or is this a JavaScript bug?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/169509-javascript-arraylength/
Share on other sites

i think what is happening is you are just removing the value of the array, but not removing the actual index of the array. try using splice: http://www.w3schools.com/jsref/jsref_splice.asp instead.

 

That's exactly what I was looking for... I knew JavaScript couldn't have had such a bug. Thanks! :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.