When I deleted an item from the array "test", the keyword delete
lead the array to become nonconsecutive. What has delete
done?
Asked
Active
Viewed 446 times
-4

Glen Pierce
- 4,401
- 5
- 31
- 50

LS.Lernord
- 81
- 2
-
3What does it look like it did? It removed the specified item but did not change other items or the array's `.length` property. (Noting that the "undefined" displayed at the end is the return value of the last `console.log()` statement, *not* a value from the array.) An array with "holes" in it (a sparse array) can also be created with `var test = []; test[3] = "d";`. – nnnnnn Mar 20 '17 at 03:27
-
delete is just delete, why asking? – Mar 20 '17 at 03:27
-
Hi @LS.Lernord. Welcome to StakOverflow. It would be great if you could post the code, and not a screenshot of it :) – surajck Mar 20 '17 at 03:28
-
When delete item from array, item is kill. – Asad Saeeduddin Mar 20 '17 at 03:28
-
@Super Cool Handsome Gel Boy, after delete test[1], the array's length is still 4. It looks like the test[1] item is replaced by "ghost" instead of truely delete. – LS.Lernord Mar 20 '17 at 05:30
-
@nnnnnn, i'm using chrome develope tool's console for this test, and it always output a undefined at the end of array when traverse it with "for". – LS.Lernord Mar 20 '17 at 05:46
-
Well,my question is duplicate of http://stackoverflow.com/questions/495764/javascript-object-with-array-deleting-does-it-actually-remove-the-item – LS.Lernord Mar 20 '17 at 06:55
-
Yes, it output "undefined" for the same reason it output "undefined" after your `var` statement. As I already said, that "undefined" has *nothing* to do with the array. Regarding that question you linked to in your comment, the accepted answer is wrong: JS arrays *can* be sparse. – nnnnnn Mar 20 '17 at 06:55
-
I'm going to hold that the "delete" just dig holes and doesn't fill them. – LS.Lernord Mar 20 '17 at 07:38
1 Answers
-2
Arrays in JavaScript are counted from 0. delete test[1]
deleted the second object in the array.

Glen Pierce
- 4,401
- 5
- 31
- 50