347

What is the difference between splice and slice ?

const array = [1, 2, 3, 4, 5];
array.splice(index, 1);
array.slice(index, 1);
4
  • 1
    check if this aswer helps you stackoverflow.com/questions/1777705/… Commented Jun 2, 2016 at 20:22
  • The splice() methods mutate an array by either adding to the array or removing from an array and returns only the removed items. Commented Jul 12, 2020 at 2:35
  • 1
    They should have interchanged names to communicate their actual meaning e.g. slice piece of cake - cut big cake and give away a portion. But in reality it does exactly opposite by keeping original cake intact and giving a slice of it, how is it possible argh.
    – vikramvi
    Commented Aug 12, 2020 at 7:38
  • 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.
    – Dean
    Commented Apr 20, 2021 at 3:31

19 Answers 19

430

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);

4
  • 41
    It's also important to observe that the slice() array method can be used to copy arrays by not passing any arguments arr1 = arr0.slice()
    – Mg Gm
    Commented Apr 6, 2019 at 11:35
  • 23
    You can think of it like splice is like a government taking taxes from you. Whereas slice is more a copy-paste guy.
    – radbyx
    Commented Jan 14, 2020 at 11:10
  • 9
    .splice() is extremely unintuitive, I just spent ages trying to figure out why references to the original array were returning undefined until I found this thread.
    – Nixinova
    Commented Jun 13, 2020 at 4:06
  • 11
    splice or like I tend to say "slice and plunder the values from the caller". Commented Apr 20, 2021 at 20:01
127

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]

3
  • 5
    Though both functions perform quite different tasks, unlike splice(x,y), in slice(x,y) the second argument y is not counted from the position of x, but from the first element of the array. Commented May 6, 2018 at 2:57
  • One more thing I noted : instead of array.slice(index, count), if you use array.slice((index, count)), you will get the remaining part after 'slicing'. Try it! Commented May 6, 2018 at 13:28
  • Edit queue is full, so: Splice and Slice both are Javascript Array METHODS. They don't exist on their own, they're both Array.prototype methods.
    – tnsaturday
    Commented Jun 16, 2022 at 11:49
74

S LICE = Gives part of array & NO splitting original array

SP LICE = Gives part of array & SPlitting original array

I personally found this easier to remember, as these 2 terms always confused me as beginner to web development.

2
  • 18
    Splice -> ctrl+X + ctrl+V Slice -> ctrl +C Commented Jul 18, 2021 at 6:53
  • 1
    It was awesome, I'll never forget that. Commented Aug 16, 2022 at 5:57
44

Here is a simple trick to remember the difference between slice vs splice

var a=['j','u','r','g','e','n'];

// array.slice(startIndex, endIndex)
a.slice(2,3);
// => ["r"]

//array.splice(startIndex, deleteCount)
a.splice(2,3);
// => ["r","g","e"]

Trick to remember:

Think of "spl" (first 3 letters of splice) as short for "specifiy length", that the second argument should be a length not an index

2
  • 5
    It's more than just how you specify arguments. One of them (splice) modifies the base array and the other one does not.
    – Arbiter
    Commented Jul 9, 2019 at 14:31
  • 2
    also could think of splice as split (generates 2 arrays) + slice
    – user7437554
    Commented Nov 6, 2019 at 1:22
28

The slice() method returns a copy of a portion of an array into a new array object.

$scope.participantForms.slice(index, 1);

This does NOT change the participantForms array but returns a new array containing the single element found at the index position in the original array.

The splice() method changes the content of an array by removing existing elements and/or adding new elements.

$scope.participantForms.splice(index, 1);

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.

3
  • Can anyone give helpful examples and what would be ideal for each? Like situations that you prefer to use splice or slice?
    – petrosmm
    Commented Jul 18, 2018 at 13:24
  • 2
    this answer is partially incorrect. for splice the 2nd arg is count of elements in return array , but for slice the 2nd arg is the index of the final element to return + 1. slice(index,1) doesn't necessarily return an array of one element starting at index. [1,2,3,4,5].slice(0,1) returns [1] but [1,2,3,4,5].slice(3,1) returns [] because 1 is interpreted as final index +1 so final index = 0 but this is before start index = 3 so empty array is returned.
    – BaltoStar
    Commented Jul 7, 2019 at 18:20
  • Why is this not the top answer?
    – JJ Labajo
    Commented Jul 30, 2019 at 9:30
19

Splice - MDN reference - ECMA-262 spec

Syntax
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Parameters

  • start: required. Initial index.
    If start is negative it is treated as "Math.max((array.length + start), 0)" as per spec (example provided below) effectively from the end of array.
  • deleteCount: optional. Number of elements to be removed (all from start if not provided).
  • item1, item2, ...: optional. Elements to be added to the array from start index.

Returns: An array with deleted elements (empty array if none removed)

Mutate original array: Yes

Examples:

const array = [1,2,3,4,5];

// Remove first element
console.log('Elements deleted:', array.splice(0, 1), 'mutated array:', array);
// Elements deleted: [ 1 ] mutated array: [ 2, 3, 4, 5 ]

// array = [ 2, 3, 4, 5]
// Remove last element (start -> array.length+start = 3)
console.log('Elements deleted:', array.splice(-1, 1), 'mutated array:', array);
// Elements deleted: [ 5 ] mutated array: [ 2, 3, 4 ]

More examples in MDN Splice examples


Slice - MDN reference - ECMA-262 spec

Syntax
array.slice([begin[, end]])
Parameters

  • begin: optional. Initial index (default 0).
    If begin is negative it is treated as "Math.max((array.length + begin), 0)" as per spec (example provided below) effectively from the end of array.
  • end: optional. Last index for extraction but not including (default array.length). If end is negative it is treated as "Math.max((array.length + begin),0)" as per spec (example provided below) effectively from the end of array.

Returns: An array containing the extracted elements.

Mutate original: No

Examples:

const array = [1,2,3,4,5];

// Extract first element
console.log('Elements extracted:', array.slice(0, 1), 'array:', array);
// Elements extracted: [ 1 ] array: [ 1, 2, 3, 4, 5 ]

// Extract last element (start -> array.length+start = 4)
console.log('Elements extracted:', array.slice(-1), 'array:', array);
// Elements extracted: [ 5 ] array: [ 1, 2, 3, 4, 5 ]

More examples in MDN Slice examples

Performance comparison

Don't take this as absolute truth as depending on each scenario one might be performant than the other.
Performance test

19

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

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

  • Splice() method can take n number of arguments:

    Argument 1: Index, Required.

    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.

  • slice() method can take 2 arguments:

    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. 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.

1
14

Splice and Slice are built-in Javascript commands -- not specifically AngularJS commands. Slice returns array elements from the "start" up until just before the "end" specifiers. Splice mutates the actual array, and starts at the "start" and keeps the number of elements specified. Google has plenty of info on this, just search.

2
  • 3
    Splice deletes the specified number and then inserts any subsequent arguments. Commented Jun 2, 2016 at 20:22
  • splice deletes a given number of elements from a given start index e.g. splice(4,1); deletes one element starting from index 4 whereas splice(4,3); deletes three elements starting with the element at index 4. Then after deleting them it returns the deleted values. Commented Nov 13, 2019 at 11:58
10

Most answers are too wordy.

  • splice and slice return rest of elements in the array.
  • splice mutates the array being operated with elements removed while slice not.

enter image description here

9

Both return same answer but:

  • SPlice will mutate your original array.
  • Slice won't mutate your original array.
2
  • 1
    they don't actually return the same thing! [0,1,2,3].splice(1,1)modifies the array, removing 1 item (second arg) starting from index 1 (1st arg) and returns the elements removed (which are [1] in this case), while [0,1,2,3].slice(1,1) makes a copy of the array, starting at index 1 (1st arg) and stops at index 1 (2nd arg), and return that, which in this case is an empty array []. So they don't both return the same thing.
    – mockingjay
    Commented Oct 14, 2021 at 9:41
  • @mockingjay, because splice is mutating. its return thing obviously is not the same as slice.
    – AmerllicA
    Commented Oct 14, 2021 at 12:45
8

The slice( ) method copies a given part of an array and returns that copied part as a new array. It doesn’t change the original array.

The splice( ) method changes an array, by adding or removing elements from it.

Here is the slice syntax:

array.slice(from, until);

// example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.slice(1, 3)
console.log({array, newArray})

// output: array: [1, 2, 3, 4, 5, 6], newArray: [2, 3]

Note: the Slice( ) method can also be used for strings.

And here is the splice syntax:

//For removing elements, we need to give the index parameter,
// and the number of elements to be removed

array.splice(index, number of elements to be removed);


//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice(1, 3)
console.log({array, newArray})

// output: array: [1, 5, 6], newArray: [2, 3, 4]

Note: If we don’t define the second parameter, every element starting from the given index will be removed from the array

// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice(index, number of elements to be removed, element, element);

//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice(1, 3, 'a', 'b')
console.log({array, newArray})

// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]

Related links:

Let’s clear up the confusion around the slice( ), splice( ), & split( ) methods in JavaScript

Array.prototype.slice()

Array.prototype.splice()

7

splice & delete Array item by index

index = 2

//splice & will modify the origin array
const arr1 = [1,2,3,4,5];
//slice & won't modify the origin array
const arr2 = [1,2,3,4,5]

console.log("----before-----");
console.log(arr1.splice(2, 1));
console.log(arr2.slice(2, 1));

console.log("----after-----");
console.log(arr1);
console.log(arr2);

let log = console.log;

//splice & will modify the origin array
const arr1 = [1,2,3,4,5];

//slice & won't modify the origin array
const arr2 = [1,2,3,4,5]

log("----before-----");
log(arr1.splice(2, 1));
log(arr2.slice(2, 1));

log("----after-----");
log(arr1);
log(arr2);

enter image description here

6

slice does not change original array it return new array but splice changes the original array.

example: var arr = [1,2,3,4,5,6,7,8];
         arr.slice(1,3); // output [2,3] and original array remain same.
         arr.splice(1,3); // output [2,3,4] and original array changed to [1,5,6,7,8].

splice method second argument is different from slice method. second argument in splice represent count of elements to remove and in slice it represent end index.

arr.splice(-3,-1); // output [] second argument value should be greater then 
0.
arr.splice(-3,-1); // output [6,7] index in minus represent start from last.

-1 represent last element so it start from -3 to -1. Above are major difference between splice and slice method.

5

Another example:

[2,4,8].splice(1, 2) -> returns [4, 8], original array is [2]

[2,4,8].slice(1, 2) -> returns 4, original array is [2,4,8]
4

//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);

1
  • Kindly provide more information on what is going on
    – Manit
    Commented Aug 28, 2019 at 2:18
4

slice and splice are intimately connected, but yet serves very different purposes:

The slice function is used to select a portion of an array. Its purpose is its return value. Its execution does not affect its subject.

The splice function is used to remove elements from an array. Its purpose is to modify its subject. It still returns a copy of the removed items, for reference, if needed.

3

The difference between Slice() and Splice() javascript build-in functions is, Slice returns removed item but did not change the original array ; like,

        // (original Array)
        let array=[1,2,3,4,5] 
        let index= array.indexOf(4)
         // index=3
        let result=array.slice(index)
        // result=4  
        // after slicing=>  array =[1,2,3,4,5]  (same as original array)

but in splice() case it affects original array; like,

         // (original Array)
        let array=[1,2,3,4,5] 
        let index= array.indexOf(4)
         // index=3
        let result=array.splice(index)
        // result=[4,5]  
        // after splicing array =[1,2,3]  (splicing affects original array)
0
2

There are 3 differences:

  1. Splice will remove the selected elements from the original array and return them as a new array -Notice that the original will no longer have them-. Slice will create a new array with the selected elements without affecting the original one.

  2. Splice receives as parameters the start index and how many elements to remove from that point. Slice receives 2 indexes, start and end.

  3. Splice can be used to add elements at a specific position in the array by passing optional parameters.

2

These two methods are very confusing for beginners. I have researched and found 4 key points in slice and splice. You can read more in detail about slice vs splice.

Slice in JavaScript

  • Slice just returns a specified number of elements from an array. For example you have an array of 10 elements and you want to just get 3 elements from 2nd index.
  • It doesn't modify the original array but returns the number of elements.
  • The syntax of the slice is array.slice(startingIndex, elementCount)

Splice in JavaScript

  • Splice can also return the selected elements from the array same as the slice but it modifies the original array.

  • You can add a new element in an array using splice on a specific index

  • You can remove specified elements as well as add new elements at the same time.

  • The syntax of using splice is array.splice(startingIndex, removeCounter, newElement(s)[optional])

Summary

The purpose of the slice is to get only selected elements from array and it doesn't modify the original array. The splice should be used when you want to modify the original array by removing elements and adding new elements.

Not the answer you're looking for? Browse other questions tagged or ask your own question.