centenial Posted August 9, 2009 Share Posted August 9, 2009 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! Quote Link to comment Share on other sites More sharing options...
abazoskib Posted August 9, 2009 Share Posted August 9, 2009 did you check the length of the array after first creating it? Quote Link to comment Share on other sites More sharing options...
centenial Posted August 9, 2009 Author Share Posted August 9, 2009 Yes. var arr = new Array(); alert(arr.length); // returns 0 Quote Link to comment Share on other sites More sharing options...
abazoskib Posted August 10, 2009 Share Posted August 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
centenial Posted August 10, 2009 Author Share Posted August 10, 2009 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.