Go Back

JavaScript: Delete and Insert

Posted on 2018-09-04

Today a really simple snippet.

Inserting an item into an existing array:

let myFruits = ["apple", "orange", "strawberry"];
myFruits.splice(1, 0, "banana");
console.log(myFruits); // [ 'apple', 'banana', 'orange', 'strawberry' ]


array.splice(index, 0, item)

at index, delete 0 items and insert item at this position.

So with splice, we can delete and insert.

Read more @MDN