JavaScript | Array slice()

arr.slice() function returns a new array containing a portion of the array on which it is implemented. The original remains unchanged. The syntax of the function is as follows:

arr.slice(begin, end)

Arguments
This function takes two optional arguments. The first argument defines the starting index from where the portion is to be extracted. If this argument is missing then the function takes begin as 0 as it is the default start value. The second argument of the function 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 function returns a new array containing some portion of the original array.

Examples for the above function are provided below:

Example 1:



var arr = [23,56,87,32,75,13];
var new_arr = arr.slice();
print(arr);
print(new_arr);

Output:

[23,56,87,32,75,13]
[23,56,87,32,75,13]

In this example the function slice() extracts the entire array from the given string and returns it as the answer, Since no arguments were passed to it.

Example 2:

var arr = [23,56,87,32,75,13];
var new_arr = arr.slice(2);
print(arr);
print(new_arr);

Output:

[23,56,87,32,75,13]
[87,32,75,13]

In this example the function slice() extracts the array starting from index 2 till the end of the array and returns it as the answer.

Example 3:

var arr = [23,56,87,32,75,13];
var new_arr = arr.slice(2,4);
print(arr);
print(new_arr);

Output:

[23,56,87,32,75,13]
[87,32]

In this example the function slice() extracts the array from the given array starting from index 2 and including all the elements less than the index 4.

Example 4:

var arr = [23,56,87,32,75,13];
var new_arr = arr.slice(3,10);
print(arr);
print(new_arr);

Output:



[23,56,87,32,75,13]
[32,75,13]

In this example the function slice() extracts the array from the index 3 up to the end of the array, since the upper limit defined is larger than the size of the array.

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

//JavaScript to illustrate slice() function
<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>

chevron_right


Output:

[23,56,87,32,75,13]
[23,56,87,32,75,13]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

//JavaScript to illustrate slice() function
<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>

chevron_right


Output:

[23,56,87,32,75,13]
[87,32,75,13]

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

//JavaScript to illustrate slice() function
<script>
function func() {
  
    //Original Array
    var arr = [23,56,87,32,75,13];
  
    //Extracted array
    var new_arr = arr.slice(2,4);
    document.write(arr);
    document.write("<br>");
    document.write(new_arr);
}
func();
</script>

chevron_right


Output:

[23,56,87,32,75,13]
[87,32]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.