Below is the example of Array slice() method.
- Example:
<script>functionfunc() {// Original Arrayvararr = [23,56,87,32,75,13];// Extracted arrayvarnew_arr = arr.slice(2,4);document.write(arr);document.write("<br>");document.write(new_arr);}func();</script>chevron_rightfilter_none - Output:
[23,56,87,32,75,13] [87,32]
The arr.slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged.
Syntax:
arr.slice(begin, end)
Parameters: This method accepts two parameters as mentioned above and described below:
- begin: This parameter defines the starting index from where the portion is to be extracted. If this argument is missing then the method takes begin as 0 as it is the default start value.
- end: This parameter is the index up to which the portion is to be extracted (excluding the end index). If this argument is not defined then the array till the end is extracted as it is the default end value If the end value is greater than the length of the array, then the end value changes to length of the array.
Return value: This method returns a new array containing some portion of the original array.
Below examples illustrate the JavaScript Array slice() method:
- Example 1: In this example the slice() method extracts the entire array from the given string and returns it as the answer, Since no arguments were passed to it.
var arr = [23,56,87,32,75,13]; var new_arr = arr.slice(); document.write(arr); document.write(new_arr);
Output:
[23,56,87,32,75,13] [23,56,87,32,75,13]
- Example 2: In this example the slice() method extracts the array starting from index 2 till the end of the array and returns it as the answer.
var arr = [23,56,87,32,75,13]; var new_arr = arr.slice(2); document.write(arr); document.write(new_arr);
Output:
[23,56,87,32,75,13] [87,32,75,13]
- Example 3: In this example the slice() method extracts the array from the given array starting from index 2 and including all the elements less than the index 4.
var arr = [23,56,87,32,75,13]; var new_arr = arr.slice(2,4); document.write(arr); document.write(new_arr);
Output:
[23,56,87,32,75,13] [87,32]
Code for the above method is provided below:
Program 1:
<script> function func() { //Original Array var arr = [23,56,87,32,75,13]; //Extracted array var new_arr = arr.slice(); document.write(arr); document.write("<br>"); document.write(new_arr); } func(); </script> |
Output:
[23,56,87,32,75,13] [23,56,87,32,75,13]
Program 2:
<script> function func() { //Original Array var arr = [23,56,87,32,75,13]; //Extracted array var new_arr = arr.slice(2); document.write(arr); document.write("<br>"); document.write(new_arr); } func(); </script> |
Output:
[23,56,87,32,75,13] [87,32,75,13]
Supported Browsers: The browsers supported by JavaScript Array slice() method are listed below:
- Google Chrome
- Microsoft Edge
- Mozilla Firefox
- Safari
- Opera
Recommended Posts:
- What is the difference between Array.slice() and Array.splice() in JavaScript ?
- TypeScript | Array slice() Method
- JavaScript | typedArray.slice() with Example
- JavaScript | string.slice()
- JavaScript arrayBuffer.slice() property
- TypeScript | String slice() Method with example
- Node.js | Buffer.slice() Method
- JavaScript | Difference between String.slice and String.substring
- JavaScript Array some() Method
- JavaScript Array from() Method
- JavaScript Array map() Method
- JavaScript Array pop() Method
- JavaScript | Array from() Method
- JavaScript | Array map() Method
- JavaScript Array every() Method
- JavaScript Array reduce() Method
- JavaScript Array flat() Method
- JavaScript Array entries() Method
- JavaScript Array findIndex() Method
- JavaScript Array splice() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


