What is the difference between slice and splice?

Barış Gündüz
2 min readMay 5, 2023

--

Some of the major differences :

1- Slice

  • Doesn’t modify the original array(immutable)
  • Returns the subset of original array
  • Used to pick the elements from array
  • Takes 2 arguments

2- Splice

  • Modifies the original array(mutable)
  • Returns the deleted elements as array
  • Used to insert or delete elements to/from array
  • It can take n number of arguments

What is the purpose of the array slice method?

The slice() method returns the selected elements in an array as a new array object. It selects the elements starting at the given start argument, and ends at the given optional end argument without including the last element. If you omit the second argument then it selects till the end.

Some of the examples of this method are,

let arrayIntegers = [1, 2, 3, 4, 5];
let arrayIntegers1 = arrayIntegers.slice(0, 2); // returns [1,2]
let arrayIntegers2 = arrayIntegers.slice(2, 3); // returns [3]
let arrayIntegers3 = arrayIntegers.slice(4); //returns [5]

Note: Slice method won’t mutate the original array but it returns the subset as a new array.

What is the purpose of the array splice method?

The splice() method is used either adds/removes items to/from an array, and then returns the removed item. The first argument specifies the array position for insertion or deletion whereas the optional second argument indicates the number of elements to be deleted. Each additional argument is added to the array.

Some of the examples of this method are,

let arrayIntegersOriginal1 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal2 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal3 = [1, 2, 3, 4, 5];

let arrayIntegers1 = arrayIntegersOriginal1.splice(0, 2); // returns [1, 2]; original array: [3, 4, 5]
let arrayIntegers2 = arrayIntegersOriginal2.splice(3); // returns [4, 5]; original array: [1, 2, 3]
let arrayIntegers3 = arrayIntegersOriginal3.splice(3, 1, "a", "b", "c"); //returns [4]; original array: [1, 2, 3, "a", "b", "c", 5]

Note: Splice method modifies the original array and returns the deleted array.

Follow me on Twitter, Instagram and if you want also on Github :) I also upload videos to Youtube from time to time.

Reference:

1- https://github.com/sudheerj

2- https://stackoverflow.com/questions/37601282/javascript-array-splice-vs-slice

--

--

Barış Gündüz
Barış Gündüz

Written by Barış Gündüz

💻 Software Engineer 🏦 Owner @gunduzmedya 🎬 Content Creator

No responses yet