Answers for "delete vs splice javascript"

1

delete vs splice javascript

/*
Delete: removes the object from the element in the array, the length of the 
array won't change. 
Splice: removes the object and shortens the array.
*/

// Using delete
arr = ['a', 'b', 'c', 'd'];
delete arr[0];
console.log(arr); // [empty, "b", "c", "d"]

// Using splice
arr = ['a', 'b', 'c', 'd'];
arr.splice(0, 1); // deletes 'a' from arr
console.log(arr); // ['b', 'c', 'd']
Posted by: Guest on July-04-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language