What is the difference between Array.slice() and Array.splice() in JavaScript ?
Last Updated :
28 May, 2024
In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements.
slice():
The slice() method in JavaScript extracts a section of an array and returns a new array containing the selected elements, without modifying the original array.
Syntax:
array_name.slice(s, e)
Example: In this example The slice() method in JavaScript extracts elements from the cars array, creating a new array new_cars from index 1 up to, but not including, index 4.
JavaScript
let cars = ['Benz', 'Innova', 'Breeza', 'Etios', 'Dzire'];
let new_cars = cars.slice(1, 4);
console.log("cars :", cars);
console.log("new_cars :", new_cars);
Outputcars : [ 'Benz', 'Innova', 'Breeza', 'Etios', 'Dzire' ]
new_cars : [ 'Innova', 'Breeza', 'Etios' ]
splice():
The splice() method in JavaScript is used to change the contents of an array by removing or replacing existing elements and/or adding new elements in place, modifying the original array.
Syntax:
array_name.splice(i, n, item 1, item 2, .....item n)
Example: In this example, The splice() method in JavaScript inserts new elements ('ambassador', 'BMW', 'Audi') into the cars array at index 2 without removing any elements.
javascript
let cars = ['Benz', 'Innova', 'Breeza', 'Etios', 'Dzire'];
cars.splice(2, 0, 'ambassedor', 'BMW', 'Audi');
console.log("cars :", cars);
Outputcars : [
'Benz', 'Innova',
'ambassedor', 'BMW',
'Audi', 'Breeza',
'Etios', 'Dzire'
]
Difference Table between Array.slice() and Array.splice()
slice() | splice() |
---|
This method is used to get a new array by selecting a sub-array of a given array. | This method is used to add/remove an item from the given array. |
The parameter 's' indicates the starting index and 'e' indicates the ending index. They denote the index of the sub-array to be taken. By default, the value for start is '0' and end is 'n'. | The parameter 'i' denotes the starting index, 'n' denotes the number of items to be removed from the specified starting index.'item 1, item 2, .....item n' represents the list of new items to be added at the given index. If n=0, no item is removed, the new items are just added to the specified starting index. |
The changes do not reflect in the original array. | The changes are reflected in the original array |
The result has to be assigned to a new array variable. | The result need not be assigned to any other new variable. |
The return value is a new array with the values in the selected sub-array of the given array. The values in the range start to (end-1) will be selected. | The return value is an array containing the deleted element. |
Takes exactly 2 arguments | Takes 'n' number of arguments (a list of new items can be supplied) |
Similar Reads
Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read
Difference Between Array.from and Array.of in JavaScript JavaScript provides various methods for creating and manipulating arrays, two of which are Array.from and Array.of. These methods are part of the ECMAScript 6 (ES6) specification and offer distinct ways to create arrays. Understanding the differences between these two methods is important for effici
3 min read
What is the difference between unshift() and Push() method in JavaScript? JavaScript Unshift() method is very much similar to the push() method but the difference is that the unshift() method adds the elements at the very beginning of the array whereas the push() method adds at the end of the array. Javascript Array unshift() method: The JavaScript Array unshift() Method
2 min read
Difference between String.slice and String.substring in JavaScript These 2 functions are quite similar in their Syntax But are different in some cases. Let's see the difference between them. JavaScript slice() Method:This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted par
3 min read
Different Ways to Use Array Slice in JavaScript In this article, we will see the different ways to use the Array slice method in Javascript. Using Array Slice in JavaScript refers to the technique of extracting a specified portion of an array, defined by start and end indices, to create a new array containing those selected elements. Syntaxarr.sl
4 min read
Difference between substr() and substring() in JavaScript In JavaScript Both of the functions are used to get the specified part of the string, But there is a slight difference between them. substr() and substring() are string methods used to extract parts of a string. The main difference is that substr() accepts a length parameter, while substring() accep
2 min read