Using Array.splice() for Removing/Adding Items in Javascript


Remove or add items to an array at any position using .splice().

Source Code

let fruits = ['apple', 'banana', 'cherry'];
fruits.splice(1, 1, 'orange', 'pear'); // Removes 'banana', adds 'orange' and 'pear'
console.log(fruits); // ["apple", "orange", "pear", "cherry"]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments