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);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Thalaivar on Stack Overflow
🌐
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.
Discussions

JavaScript Array splice vs slice - Stack Overflow
The splice() method changes the content of an array by removing existing elements and/or adding new elements. ... This will remove one element from the participantForms array at the index position. These are the Javascript native functions, AngularJS has nothing to do with them. More on stackoverflow.com
🌐 stackoverflow.com
Help with splicing an array into another
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); return localArr; } I tried to shorten it with: function frankenSplice(arr1, ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
4
0
March 24, 2022
A better way to splice an array into an array in javascript - Stack Overflow
The splice function modifies the array directly, but returns the an array of elements that were removed... not the spliced array. While it's normally not recommended to extend core javascript classes, this is relatively benign with most standard frameworks. More on stackoverflow.com
🌐 stackoverflow.com
javascript - Remove element from array (splice) - Stack Overflow
Basic question on .splice() method, and how best to remove an element from an array. I want to remove an item from an array with .splice() but when I do, I want to have the original array minus the More on stackoverflow.com
🌐 stackoverflow.com
🌐
Refine
refine.dev › home › blog › tutorials › how to use javascript array splice
How to Use JavaScript Array Splice | Refine
September 5, 2024 - As we'll start seeing next, splice() mutates the original array and returns an array containing removed items. Although, JavaScript splice() attempts to mutate the caller array, the returned value is an array of removed items.
Top answer
1 of 16
434

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);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

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]
Run code snippetEdit code snippet Hide Results Copy to answer Expand

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]
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
CodingNomads
codingnomads.com › javascript-array-splice-insert-delete-replace
How to Replace, Add or Delete Array Items: JavaScript Splice
In this example, .splice() is called at index 1, told to remove 0 items, and add in "purple" at index 1. The end result is an array with an extra item. The splice() method in JavaScript mutates, or changes, an array in place.
🌐
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
Find elsewhere
🌐
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 - 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 as...
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Help with splicing an array into another - Curriculum Help - The freeCodeCamp Forum
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)…
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › dynamic array manipulation with javascript's splice()
Dynamic Array Manipulation with JavaScript splice() | John Kavanagh
January 15, 2023 - Discover how to master array modifications with JavaScript's Array.prototype.splice(), your all-in-one method for removing, adding, and replacing elements.
🌐
Medium
medium.com › @ryan_forrester_ › javascripts-splice-method-a-complete-guide-c579ff14af7a
JavaScript’s splice() Method: A Complete Guide | by ryan | Medium
November 19, 2024 - Unlike simpler methods that only add or remove elements, splice() lets you modify arrays in place by removing existing elements and inserting new ones simultaneously.
🌐
DEV Community
dev.to › rfornal › javascript-array-splice-issue-5301
JavaScript Array Splice Issue - DEV Community
September 17, 2019 - When dealing with removing an item from the array, I generally like to do an immutable version for the reason described above in the post. So instead of · const arr = ['one', 'two', 'three', 'four', 'five']; arr.splice(arr.indexOf(name), 1);
🌐
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.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-slice-and-splice-how-to-use-the-slice-and-splice-js-array-methods
How to Use the slice() and splice() JavaScript Array Methods
April 13, 2022 - The splice() method is used to add or remove elements of an existing array and the return value will be the removed items from the array. If nothing was removed from the array, then the return value will just be an empty array.
🌐
1SMARTchicken
1smartchicken.com › reference › javascript-array-splice-method
JavaScript - Array splice() Method | 1SMARTchicken
September 5, 2024 - // array let cars = ['Lamborghini', 'Ferrari', 'Maserati', 'Alfa Romeo']; // at position 2, add 2 elements cars.splice(2, 0, 'Astin Martin', 'McLaren'); // output the array items to the HTML element document.getElementById('my_cars').innerHTML = cars;
🌐
Medium
medium.com › @mdsiaofficial › what-splice-method-can-do-in-the-array-javascript-01064264befa
What splice() method can do in the array? (JavaScript) | by Md Shoriful Islam Ashiq | Medium
December 25, 2023 - const colors = ["red", "green", "blue"]; colors.splice(1, 1, "yellow"); // Replace one element starting at index 1 console.log(colors); // Output: ["red", "yellow", "blue"] ... It’s useful for various array manipulations like removing, adding, or replacing elements.
🌐
Sumudu Siriwardana
sumudusiriwardana.hashnode.dev › slice-and-splice-javascript-arrays-like-a-pro
Slice and Splice JavaScript Arrays Like a Pro
June 15, 2022 - The slice() method is very useful for cloning an array, copying a portion of an array, and converting an array-like object into an array. The splice() method helps you add, update, and remove elements in an array. This method modifies the array ...
🌐
DEV Community
dev.to › nameismani › unveiling-the-magic-of-javascript-arrays-a-deep-dive-into-the-splice-method-kfk
👨‍💻 Splice method in JavaScript : Unveiling the Magic of JavaScript Arrays - DEV Community
March 1, 2024 - In this article, we embark on a journey to uncover the secrets and nuances of this exceptional method, exploring its ability to create, delete, and update elements within arrays. ... The splice() method adds and/or removes array elements.
🌐
DEV Community
dev.to › hkp22 › exploring-javascript-array-methods-a-deep-dive-into-slice-and-splice-j93
Exploring JavaScript Array Methods: A Deep Dive into `.slice()` and `.splice()` - DEV Community
September 26, 2024 - Use .slice() when you want to extract part of an array without affecting the original. Use .splice() when you need to make direct changes to an array, such as removing, inserting, or replacing elements.