# Demystifying slice, splice and split

Let's talk about splice, slice and split methods. I find the names too identical and confusing at least for me. so I'll write it out to try and understand

# slice

The splice() method returns a shallow copy of an array without mutating the original array selected from begin to end (end not included).

syntax

``````arr.slice(begin, end)
``````

parameters;
begin
-We start counting from 0 as usual with arrays, likewise, if begin is undefined.
-A negative index can be used and in this case, we start counting from the end of the array. slice(-2) extracts the last two elements in the array.
-If begin is greater than the length of the array, an empty array is returned

end
-The end is never included. For example, slice(2,5) extracts the third element through the fifth element (elements indexed 2,3,4).
-A negative index can be used and in that case, we start counting from the end of the array. For example, slice(1,-2), we count the 2nd element and the 2nd last element from the end of the array, except that won't be included in the new array
-If end is omitted or end is greater than the length of the array, slice extracts through the end of the sequence (arr.length)

# splice

The splice() methods mutate an array by either adding to the array or removing from an array and returns the removed items.

syntax

``````array.splice(index, howmany, item1, ....., itemX)
``````

parameters;
index
Index at which to start changing the array (with origin 0) (required)

howmany
The number of items to be removed. If set to 0 or negative, no items will be removed (optional)

items
the items to be added to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.

some illustrations;
Inserts at 1st index position

Insert elements into the array

Remove elements from an array

the above example can also be done as;

remove an element and insert a new element

# split

The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string and returns an Array of strings split at each point where the separator occurs in the given string.

syntax

``````string.split(separator, limit)
``````

parameter;
separator
Specifies the string which denotes the points at which each split should occur. If an empty string ("") is used as the separator, the string is split between each character. The split method does not mutate the original string. (Optional)

limit
An integer that specifies the number of splits, the split() method splits the string at each occurrence of the specified separator but stops when limit entries have been placed into the array. (Optional)