delete vs splice javascript

Solutions on MaxInterview for delete vs splice javascript by the best coders in the world

showing results for - "delete vs splice javascript"
Cathy
29 Nov 2017
1/*
2Delete: removes the object from the element in the array, the length of the 
3array won't change. 
4Splice: removes the object and shortens the array.
5*/
6
7// Using delete
8arr = ['a', 'b', 'c', 'd'];
9delete arr[0];
10console.log(arr); // [empty, "b", "c", "d"]
11
12// Using splice
13arr = ['a', 'b', 'c', 'd'];
14arr.splice(0, 1); // deletes 'a' from arr
15console.log(arr); // ['b', 'c', 'd']