×

Difference Between SLICE and SPLICE in JavaScript

Difference between SLICE & SPLICE in JavaScript

Hello Devs,

In this article, we will discuss what's the difference between the two most important methods of Array in JavaScript (i.e Slice and Splice)

The splice() method

The slice() method

The splice() method

slice() method

Lets see some of the simple examples to get the small clarification.

let arr1 = [1, 2, 3, 4, 5]; let arr2 = [1, 2, 3, 4, 5]; arr1.splice(1, 3); //returns [2, 3, 4] console.log(arr1); //[1, 5] arr2.slice(1, 3); //returns [2, 3] console.log(arr2); //[1, 2, 3, 4, 5]

Lets discuss these two methods in more details.

First we will discuss the splice method in detail.

Syntax:

splice(start, deleteCount, item1, item2, ...itemN);

where, start (required) -> where you want to start editing the array.

deleteCount(optional) -> number of element you want to delete from start index

lets see some of examples.

let a = [1, 2, 3, 4, 5]; a.splice(); //[] console.log(a); // [1,2,3,4,5] let a = [1, 2, 3, 4, 5]; a.splice(2); //returns [3,4,5] console.log(a); //[1,2] let a = [1, 2, 3, 4, 5]; a.splice(-3); //returns [3,4,5], here start is treated as array.length-start console.log(a); //[1,2] let a = [1, 2, 3, 4, 5]; a.splice(-7); //returns [1,2,3,4,5], here start is treated as array.length-start and this is ngative so start will now be treated as 0 console.log(a); //[] //(an empty array) let a = [1, 2, 3, 4, 5]; a.splice(2, 9); //returns [3,4,5] console.log(a); //[1,2] let a = [1, 2, 3, 4, 5]; a.splice(2, 2, "a", "b", "c", "d"); //returns [3, 4] console.log(a); //[1, 2, "a", "b", "c", "d", 5] //slice has removed 2 elements starting from index '2' and added the item1, item2, ...itemN at start positon //This also very useful when you want to delete a specific element from an array let arr =[1,3,6,4] //we want to remove 3 from array const index = arr.indexOf(3) //returns 1 //NOTE: indexOf returns -1 if element is not present in an array and it returns the index of 1st occurrence of that element and its case-sensitive (useful when we are searching a word in string) if(index > -1){ arr.splice(index, 1) } console.log(arr) // [1,6,4]

Now lets discuss the slice method.

This method just extract the a part from an array.

Syntax:

slice(start, end);

where, start(required) -> starting index from where to start the extraction from an array.

end (optional)-> denotes till which index (excluding) you want to extract from the start,

Lets see some of the examples,

let arr = [1, 2, 3, 4, 5]; arr.slice(); //returns [1,2,3,4,5] arr.slice(2); //returns [3,4,5] console.log(arr); //[1,2,3,4,5] arr.slice(2, 1); //returns [] console.log(arr); //[1,2,3,4,5] arr.slice(2, -1); //returns [3,4], here end is treated as arr.length-1 which is 4 i.e arr.slice(2,4) console.log(arr); //[1,2,3,4,5] arr.slice(2, 9); //[3,4,5] console.log(arr); //[1,2,3,4,5]

Using slice we can also convert an array like objects to an array ? Syntax:

Array.prototype.slice.call(arguments)

example,

let array_like_obj = { 0: "john", 1: "doe", 2: "capscode", length: 3, }; console.log(Array.prototype.slice.call(array_like_obj)); //["john", "doe", "capscode"]

Thank you for reading this far. This is a brief introduction of Difference between SLICE & SPLICE method on Array in JS . If you find this article useful, like and share this article. Someone could find it useful too. If you find anything technically inaccurate please feel free to comment below. Now you can also play around the objects in JS.

Hope its a nice and informative read for you. VISIT https://www.capscode.in/blog TO LEARN MORE...

IF MY ARTICLE HELPED YOU

Buy Me A Coffee

Thanks,
CapsCode