So in this blog, I will be talking about the differences between them and technique using which you won’t get confused anymore.
- Returns the removed items.
- Changes the original array.
- Accepts 3 arguments:
- Index: required
- Count: optional – Num of items to be removed from the index. If not given all the items starting from the index till the end will be removed.
- New items to be added: optional
It can be used to remove and add items to an array.
Remove all items with Index argument only-
Remove selected items with Index and Count arguments-
If the Count argument-
- Not provided, then by default all the items would be removed starting from Index argument.
- Value is less than 0, then no item would be removed and array remains as it is.
Remove selected items and add new items with Index, Count and new items arguments-
- Returns the selected items as this method does not remove and changes the original array.
- Does not changes the original array.
- 2 arguments
- Start index: required – included
- End index: optional – not included, if not given all the items from start index till the end will be removed.
It can also be used to copy one array to another.
With Start index argument only-
Returns the selected items starting from start index as and does not changes the original array
With start index and end index arguments-
Slice() will select value starting from start index till end index and value at end index will not be included.
Positive end index
Negative end index
If you give a negative end index value, then JS will start counting from the end. In this case, we have provided an end index = -3
- Value 5 (4th index will be considered as 1 value)
- Value 4 (3rd index will be considered as 2 value)
- Value 3 (2nd index will be considered as 3 value)
So slice method will select value between index 1 and 2, therefore the output is 2.
Now, if you see, the difference between them is not much and they also sound almost the same which in turn increases the confusion.
You can remember them by their literal meaning.
Slice can be defined as a small piece cut from the large portion. So in JS also slice returns the selected items from a large array.
Splice can be defined as a join between two ropes. So in JS, you can use splice to join two arrays at a defined index.
Thank you for reading 🙂