DEV Community

Cover image for Differences Between Slice and Splice : javascript
sagar
sagar

Posted on

Differences Between Slice and Splice : javascript

In Javascript slice and splice are used to manipulate Array, both sound a little bit the same but they work differently, first let’s talk about slice

Image description

slice method used cut out some portion from the array or extract sub-array but it don’t change the origin array instead return new updated array of extracted sub-array as you can see in above example

it takes two argument first starting index , second ending index . The ending index default value is length of its original array if you don’t pass the ending index.

Image description

splice method use to remove and add values in the array , but it don’t create new array like splice instead it changed the original array as you can see above example

it take three argument first : starting index , second : number of values to be remove , here is a point to be noted its second argument is not ending index but the length of value you want to delete from the array , third: value needs to be added in replacement of removed values

conclusion: both used manipulate array , slice extract sub-array and create new array of extracted value it don’t changed original array and splice add and remove array values but changed original array.

Top comments (0)