
Take note that Array.slice() has been used before Array.splice() because we do not want to alter the original array. Let’s look at more examples using Array.splice() method.

We have first extracted the thirdPart using list.splice(-threePartIndex), which removes last 3 elements, at this point list contain only first 6 elements.

splice( - threePartIndex) Ĭonst secondPart = list. The function returns a new array containing the copied values.Const list = Ĭonst threePartIndex = Math. If end is greater than the sequence length, slice extracts through to the end of the sequence.For example, slice(1,-1) extracts the first element through the second-to-last element in the sequence. If negative, it extracts until the end of the sequence.If not supplied, it extracts to the end of the sequence.end (optional) : The ending index of the copied array.If start is greater than the index range of the sequence, an empty array is returned.For example, slice(-2) extracts the last two elements in the sequence. If negative, it indicates an offset from the end of the sequence.If not supplied, it starts from index 0.start (optional): The starting index of the copied array.The slice() method takes the following parameters:

It saves this copied portion into a new array but does not modify the original array. The slice() method in JavaScript returns a shallow copy of an array’s selected portion.
