JavaScript Array slice()
Syntax & Examples
Array.slice() method
The slice() method of the Array class in JavaScript extracts a section of the calling array and returns a new array.
Syntax of Array.slice()
There are 3 variations for the syntax of Array.slice() method. They are:
slice()
This method extracts a section of the calling array from the beginning and returns a new array.
Returns value of type Array
.
slice(start)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | optional | The beginning index of the section to extract. Defaults to 0. |
This method extracts a section of the calling array from the start index to the end of the array and returns a new array.
Returns value of type Array
.
slice(start, end)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | optional | The beginning index of the section to extract. Defaults to 0. |
end | optional | The end index of the section to extract. Defaults to the array's length. The end index is not included in the extracted section. |
This method extracts a section of the calling array from the start index to the end index (not included) and returns a new array.
Returns value of type Array
.
✐ Examples
1 Using slice() method to extract a section of an array
In JavaScript, we can use the slice() method to extract a section of an array and return a new array.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We use the slice() method on arr with the start index 1 and the end index 3 to extract a section of the array.
- The result is stored in the variable slicedArr.
- We log slicedArr to the console using console.log() method to see the extracted section of the array.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
const slicedArr = arr.slice(1, 3);
console.log(slicedArr);
Output
[2, 3]
2 Using slice() method to extract from a specific start index to the end of an array
We can use the slice() method to extract a section of an array from a specific start index to the end of the array and return a new array.
For example,
- We define an array variable arr with elements ['a', 'b', 'c', 'd', 'e'].
- We use the slice() method on arr with the start index 2 to extract a section of the array from index 2 to the end.
- The result is stored in the variable slicedArr.
- We log slicedArr to the console using console.log() method to see the extracted section of the array.
JavaScript Program
const arr = ['a', 'b', 'c', 'd', 'e'];
const slicedArr = arr.slice(2);
console.log(slicedArr);
Output
['c', 'd', 'e']
3 Using slice() method to make a shallow copy of an array
We can use the slice() method without arguments to make a shallow copy of an array and return a new array.
For example,
- We define an array variable arr with elements [10, 20, 30].
- We use the slice() method on arr without any arguments to make a shallow copy of the array.
- The result is stored in the variable copiedArr.
- We log copiedArr to the console using console.log() method to see the shallow copy of the array.
JavaScript Program
const arr = [10, 20, 30];
const copiedArr = arr.slice();
console.log(copiedArr);
Output
[10, 20, 30]
Summary
In this JavaScript tutorial, we learned about slice() method of Array: the syntax and few working examples with output and detailed explanation for each example.