5

Is there any reason why one should be used over the other?

e.g.

var arData=['a','b','c'];
arData.slice(1,1);//removes 'b'

var arData=['a','b','c'];
delete arData[1];//removes 'b'
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Francisc
  • 77,430
  • 63
  • 180
  • 276
  • possible duplicate of [JavaScript Array Delete Elements](http://stackoverflow.com/questions/500606/javascript-array-delete-elements) – PeeHaa Apr 25 '12 at 23:08

2 Answers2

30

delete leaves you with [ 'a', undefined, 'c' ]

splice leaves you with [ 'a', 'c' ]

slice doesn't do anything to the original array :) But it returns [ 'b' ] in your code

6

delete only makes that certain location of the array undefined but the array still contains 3 items: ['a',undefined,'c']

the other way to do it is splice and not slice. splice totally removes that item and it's location, so you end up with ['a','c']

Joseph
  • 117,725
  • 30
  • 181
  • 234