.slice()
The .slice()
array method returns a shallow copy of all or part of an array without modifying the original. A shallow copy duplicates the contents of an object into a new instance by reference; which is why changes to the copy are not reflected in the original.
Syntax
The returned array contains the element specified by the first argument and all subsequent elements up to, but not including, the element specified by the second argument.
array.slice(start, end);
start
: The start index of the slice to be returned (optional)end
: The end index of the slice to be returned (optional)
If only one argument is specified, the returned array contains all elements from the start position to the end of the array.
array.slice(start);
If start
and end
values are not provided, the slicing of array will be of the whole array.
array.slice();
Two Arguments
To create a subarray of ['Tuesday', 'Wednesday', 'Thursday']
from weekDays
array:
const weekDays = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',];const outOutOffice = weekDays.slice(1, 4);console.log(outOutOffice);// Output: ['Tuesday', 'Wednesday', 'Thursday']
Negative Arguments
A negative index can be used, indicating an offset from the end of the sequence. For example:
array.slice(-3, -1);
To create the same subarray as the one above with negative index values:
const weekDays = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',];const outOutOffice = weekDays.slice(-6, -3);console.log(outOutOffice);// Output: ['Tuesday', 'Wednesday', 'Thursday']
One Argument
To create a subarray of the last two elements from weekDays
:
const weekDays = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',];const weekend = weekDays.slice(5);console.log(weekend);// Output: ['Saturday', 'Sunday']
Zero Arguments
To create an identical subarray of weekDays
:
const weekDays = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',];const anotherWeek = weekDays.slice();console.log(anotherWeek);// Output: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']