Remove, insert, and replace array elements with JavaScript splice()
array.splice(start, deleteCount, ...items) edits the original JavaScript array. It can remove elements and insert new ones at the same position. The return value is an array of removed elements.
Set the removal count and read the return value
The first argument is a zero-based index. The second is a count. splice(2, 3) removes three elements starting at index 2. Negative starts count back from the end, and an excessive count removes only what is available. Removing nothing returns []. See splice().
const items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six'];
const removed = items.splice(2, 3);
console.log(removed); // ['Three', 'Four', 'Five']
console.log(items); // ['One', 'Two', 'Six']
Insert or replace at the same position
From the third argument onward list items to insert. Set the removal count to 0 to insert without deleting. A different number of additions and removals changes the length.
const colors = ['red', 'blue', 'yellow'];
const replaced = colors.splice(1, 1, 'green', 'purple');
console.log(replaced); // ['blue']
console.log(colors); // ['red', 'green', 'purple', 'yellow']
colors.splice(2, 0, 'black');
console.log(colors); // ['red', 'green', 'black', 'purple', 'yellow']
splice(start) removes everything from that position. Passing undefined explicitly as the second argument instead gives a count of 0. Use explicit 0 when inserting without removing.
Try a complete editing and reset example
Save the complete file in UTF-8 as array-splice-example.html and open it. The default operation removes Two and Three, inserts Seven and Eight, and leaves One, Seven, Eight, Four, Five, Six. A blank start means 0. Leave the count blank to delete through the end, with no replacement items. Reset restores the original list and inputs.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Practice removing and inserting elements</title>
</head>
<body>
<p>Initial array: One, Two, Three, Four, Five, Six</p>
<label for="start">Start index</label>
<input id="start" type="number" step="1" value="1">
<label for="count">Number to remove (blank to remove through the end)</label>
<input id="count" type="number" min="0" step="1" value="2">
<label for="add">Values to insert (comma-separated)</label>
<input id="add" type="text" value="Seven, Eight">
<button id="apply" type="button">Edit array</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const original = ['One', 'Two', 'Three', 'Four', 'Five', 'Six'];
let items = original.slice();
const startInput = document.querySelector('#start');
const countInput = document.querySelector('#count');
const addInput = document.querySelector('#add');
const output = document.querySelector('#output');
function display(removed) {
output.textContent = `Current array: ${JSON.stringify(items)}\n` +
`Removed elements: ${JSON.stringify(removed)}\n` +
`Current length: ${items.length}`;
}
document.querySelector('#apply').addEventListener('click', () => {
const start = Number(startInput.value);
const countText = countInput.value.trim();
const additions = addInput.value.trim() === '' ? [] :
addInput.value.split(',').map(item => item.trim());
if (startInput.validity.badInput || countInput.validity.badInput ||
!Number.isInteger(start) ||
(countText !== '' && (!Number.isInteger(Number(countText)) || Number(countText) < 0))) {
output.textContent = 'Enter an integer start and a nonnegative integer removal count.';
return;
}
if (countText === '' && additions.length > 0) {
output.textContent = 'Enter the removal count when inserting values. Use 0 for insertion only.';
return;
}
const removed = countText === ''
? items.splice(start)
: items.splice(start, Number(countText), ...additions);
display(removed);
});
document.querySelector('#reset').addEventListener('click', () => {
items = original.slice();
startInput.value = '1';
countInput.value = '2';
addInput.value = 'Seven, Eight';
display([]);
});
display([]);
</script>
</body>
</html>
Preserve the original with toSpliced()
toSpliced() returns the edited array and keeps the input. With splice(), the same operation returns ['B'] and modifies its receiver. Run this comparison in a browser supporting toSpliced(). See toSpliced().
{
const original = ['A', 'B', 'C'];
const edited = original.toSpliced(1, 1, 'X');
const inPlace = ['A', 'B', 'C'];
const removed = inPlace.splice(1, 1, 'X');
console.log(edited, original); // ['A', 'X', 'C'], ['A', 'B', 'C']
console.log(removed, inPlace); // ['B'], ['A', 'X', 'C']
}
If toSpliced() is unavailable, make a shallow copy with slice() and apply splice() to the copy. The arrays still share object elements, so changing an object property takes effect through both references.
const base = ['A', 'B', 'C'];
const copy = base.slice();
copy.splice(1, 1, 'X');
console.log(base); // ['A', 'B', 'C']
console.log(copy); // ['A', 'X', 'C']




