Deleting array elements in JavaScript - delete vs splice

后端 未结 27 4443
予麋鹿
予麋鹿 2020-11-21 05:31

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method?

For example:

myArray = [\         


        
27条回答
  •  佛祖请我去吃肉
    2020-11-21 06:26

    From Core JavaScript 1.5 Reference > Operators > Special Operators > delete Operator :

    When you delete an array element, the array length is not affected. For example, if you delete a[3], a[4] is still a[4] and a[3] is undefined. This holds even if you delete the last element of the array (delete a[a.length-1]).

提交回复
热议问题