Array.splice() is a JavaScript method that modifies an array in place by removing, replacing, or adding elements. It changes the original array and returns an array of the removed elements (or an empty array if none were removed).

Key Features

  • Mutating: Directly alters the original array.

  • Flexible Operations: Can remove elements, insert new ones, or replace existing elements in a single call.

  • Returns Removed Elements: The method returns an array containing the deleted elements, or an empty array if no elements were removed.

Syntax

array.splice(startIndex, deleteCount, item1, item2, ..., itemN)
  • startIndex: Zero-based index where changes begin. Can be negative to count from the end.

  • deleteCount: Number of elements to remove. If omitted, removes all elements from startIndex to the end.

  • item1, item2, ...: Elements to add to the array starting at startIndex. If deleteCount is 0, these are inserted without removing anything.

Examples

  • Insert at the beginning:

    const arr = [1, 2, 3];
    arr.splice(0, 0, 0); // [0, 1, 2, 3]
  • Insert at the end:

    arr.splice(arr.length, 0, 4); // [0, 1, 2, 3, 4]
  • Replace an element:

    arr.splice(1, 1, 'X'); // [0, 'X', 2, 3, 4]
  • Remove elements:

    const removed = arr.splice(2, 2); // removed = [2, 3], arr = [0, 'X', 4]

Important Notes

  • Negative indices count backward from the end of the array.

  • deleteCount of 0 means no removals, only insertion.

  • Returns an array of removed elements; use slice() if you want a copy without mutation.

  • Not suitable for strings, as they are immutable.

For non-mutating alternatives, use toSpliced() (ES2022+) to create a new array with changes.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
🌐
W3Schools
w3schools.com › jsref › jsref_splice.asp
W3Schools.com
The splice() method adds and/or removes array elements.
Discussions

Help with splicing an array into another
It's alive! let localArr = ... function frankenSplice(arr1, arr2, n) { return arr2.splice(n, 0, ...arr1); } console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1)); But this doesn’t work. It just returns an empty array.... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
March 24, 2022
php - array_splice() for associative arrays - Stack Overflow
Say I have an associative array: array( "color" => "red", "taste" => "sweet", "season" => "summer" ); and I want to introduce a new element into it: "texture" => "bumpy" behind... More on stackoverflow.com
🌐 stackoverflow.com
JavaScript Array splice vs slice - Stack Overflow
When you have a sandwich, what you really do is have two splices, not two slices. You will have reduced the loaf by doing so. Unintuitive, I know. ... This will return [3,4,5]. The original array is affected resulting in array being [1,2]. More on stackoverflow.com
🌐 stackoverflow.com
Why does array.splice(); works like it works? WHY?
I don't know....I think this one's easy to remember because it's like splicing genes, innit? start here, end there, remove that bit - like you're cutting with a scalpel. More on reddit.com
🌐 r/learnjavascript
68
1
July 5, 2019
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
1 week ago - This example uses the splice() method to remove the strings "Banana" and "Strawberry" from the fruits array — by specifying the index position of "Banana", along with a count of the number of total items to remove.
🌐
p5.js
p5js.org › reference › p5 › splice
splice
Copies an array (or part of an array) to another array.
🌐
PHP
php.net › manual › en › function.array-splice.php
PHP: array_splice - Manual
The returned arrays is the 2nd argument actually and the used array e.g $input here contains the 1st argument of array, e.g <?php $input = array("red", "green", "blue", "yellow"); print_r(array_splice($input, 3)); // Array ( [0] => yellow ) print_r($input); //Array ( [0] => red [1] => green [2] => blue ) ?> if you want to replace any array value do simple like that, first search the array index you want to replace <?php $index = array_search('green', $input);// index = 1 ?> and then use it as according to the definition <?php array_splice($input, $index, 1, array('mygreeen')); //Array ( [0] => red [1] => mygreeen [2] => blue [3] => yellow ) ?> so here green is replaced by mygreen. here 1 in array_splice above represent the number of items to be replaced. so here start at index '1' and replaced only one item which is 'green' ... array_splice() does not preserve numeric keys.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-splice-method
JavaScript Array splice() Method - GeeksforGeeks
The splice() method in JavaScript is used to change the contents of an array by removing, replacing, or adding elements.
Published   January 16, 2026
🌐
Metana
metana.io › metana: coding bootcamp | software, web3 & cyber › full-stack › javascript array splice() method - metana
JavaScript Array Splice() Method - Metana
November 10, 2024 - You can even use negative values to count backward from the end of the array. deleteCount (Optional): This specifies the number of elements you want to remove starting from the startIndex. If omitted, no elements are removed. item1, item2, … itemN (Optional): These are the new elements you want to insert at the startIndex. Any number of items can be added here. Important Note: splice() modifies the original array directly.
Find elsewhere
🌐
Refine
refine.dev › home › blog › tutorials › how to use javascript array splice
How to Use JavaScript Array Splice | Refine
September 5, 2024 - And passing the items list depends on whether/what we want to insert or add to the array. It is also possible to pass all arguments at the same time. In the coming sections, we consider most of the cases. As we'll start seeing next, splice() mutates the original array and returns an array containing removed items.
🌐
W3Schools
w3schools.com › php › func_array_splice.asp
PHP array_splice() Function
The array_splice() function removes selected elements from an array and replaces it with new elements.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-array_splice-function
PHP array_splice() Function - GeeksforGeeks
September 24, 2024 - <?php // PHP program to illustrate the use // of array_splice() function $array1 = array("10"=>"raghav", "20"=>"ram", "30"=>"laxman","40"=>"aakash","50"=>"ravi"); $array2 = array("60"=>"ankita","70"=>"antara"); echo "The returned array: \n"; print_r(array_splice($array1, 1, 4, $array2)); echo "\nThe original array is modified to: \n"; print_r($array1); ?>
🌐
Mimo
mimo.org › glossary › javascript › splice
JavaScript Splice Method: Array Modification Techniques
The splice() method in JavaScript changes the contents of an array by removing, replacing, or adding elements.
🌐
Codecademy
codecademy.com › docs › php › arrays › array_splice()
PHP | Arrays | array_splice() | Codecademy
August 30, 2023 - The array_splice() method takes an array as its input value and replaces elements within the array with new elements.
🌐
DEV Community
dev.to › dillionmegida › arraysplice-for-removing-replacing-or-adding-values-to-an-array-1k6c
Array.splice() - for removing, replacing or adding values to an array - DEV Community
May 18, 2022 - From index 1 (where the value 2 is), the starting position, splice deletes 2 items (the second argument) which are 2 and 3. The removed values are returned in an array, and the original array is modified as the values are removed.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Help with splicing an array into another
March 24, 2022 - So, doing one of the challenges, I saw that this was the solution on the help section: function frankenSplice(arr1, arr2, n) { // It's alive. It's alive! let localArr = arr2.slice(); localArr.splice(n, 0, ...arr1)…
🌐
freeCodeCamp
freecodecamp.org › news › javascript-splice-how-to-use-the-splice-js-array-method
JavaScript Splice – How to Use the .splice() JS Array Method
April 23, 2021 - The splice() method is a built-in method for JavaScript Array objects. It lets you change the content of your array by removing or replacing existing elements with new ones. This method modifies the original array and returns the removed elements ...
Top answer
1 of 16
433

splice() changes the original array whereas slice() doesn't but both of them returns array object.

See the examples below:

var array=[1,2,3,4,5];
console.log(array.splice(2));

This will return [3,4,5]. The original array is affected resulting in array being [1,2].

var array=[1,2,3,4,5]
console.log(array.slice(2));

This will return [3,4,5]. The original array is NOT affected with resulting in array being [1,2,3,4,5].

Below is simple fiddle which confirms this:

//splice
var array=[1,2,3,4,5];
console.log(array.splice(2));

//slice
var array2=[1,2,3,4,5]
console.log(array2.slice(2));


console.log("----after-----");
console.log(array);
console.log(array2);

2 of 16
130

Splice and Slice both are Javascript Array functions.

Splice vs Slice

  1. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.

  2. The splice() method changes the original array and slice() method doesn’t change the original array.

  3. The splice() method can take n number of arguments and slice() method takes 2 arguments.

Splice with Example

Argument 1: Index, Required. An integer that specifies at what position to add /remove items, Use negative values to specify the position from the end of the array.

Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed.

Argument 3…n: Optional. The new item(s) to be added to the array.

var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
 
console.log(array);
// shows [1, 2], original array altered.
 
var array2=[6,7,8,9,0];
console.log(array2.splice(2,1));
// shows [8]
 
console.log(array2.splice(2,0));
//shows [] , as no item(s) removed.
 
console.log(array2);
// shows [6,7,9,0]

Slice with Example

Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array.

Argument 2: Optional. An integer that specifies where to end the selection but does not include. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.

var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
 
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.
 
var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
// shows [8, 9]
 
console.log(array2.slice(-2,4));
// shows [9]
 
console.log(array2.slice(-3,-1));
// shows [8, 9]
 
console.log(array2);
// shows [6, 7, 8, 9, 0]

🌐
Reddit
reddit.com › r/learnjavascript › why does array.splice(); works like it works? why?
r/learnjavascript on Reddit: Why does array.splice(); works like it works? WHY?
July 5, 2019 -

Hello,

so in my honest opinion the Array.prototype.splice makes no sense to me. Why didn't they just add a nice method like "remove" "update" "swap" instead of "splice"? Who's thinking of those stupid names and why did they put all 3 "functions" into this one method?

I've created my own "remove from array" method:

Array.prototype.remove = function(value){
  var index = this.indexOf(value);
  this.splice(index,1);
}

EDIT;
I found something on stack overflow.
https://stackoverflow.com/questions/6515385/alternate-method-to-splice-function-in-javascript

Array.prototype.remove =  function(value, deleteCount) {
  var start = array.indexOf(value);
  var result = [];
  var removed = [];
  var argsLen = arguments.length;
  var arrLen = this.length;
  var i, k;

  // Follow spec more or less
  deleteCount = parseInt(deleteCount, 10);

  // Deal with negative start per spec
  // Don't assume support for Math.min/max
  if (start < 0) {
    start = arrLen + start;
    start = (start > 0)? start : 0;
  } else {
    start = (start < arrLen)? start : arrLen;
  }

  // Deal with deleteCount per spec
  if (deleteCount < 0) deleteCount = 0;

  if (deleteCount > (arrLen - start)) {
    deleteCount = arrLen - start;
  }

  // Copy members up to start
  for (i = 0; i < start; i++) {
    result[i] = this[i];
  }

  // Add new elements supplied as args
  for (i = 3; i < argsLen; i++) {
    result.push(arguments[i]);
  }

  // Copy removed items to removed this
  for (i = start; i < start + deleteCount; i++) {
    removed.push(this[i]);
  }

  // Add those after start + deleteCount
  for (i = start + (deleteCount || 0); i < arrLen; i++) {
    result.push(this[i]);
  }

  // Update original this
  this.length = 0;
  i = result.length;
  while (i--) {
    this[i] = result[i];
  }

  // Return this of removed elements
  return removed;
}