Horje
What is Array.prototype.slice() method in JavaScript ?

The Array.prototype.slice() method in JavaScript is used to extract a portion of an array and create a new array containing the selected elements. It does not modify the original array. instead, it returns a shallow copy of a portion of the array.

Syntax:

array.slice(startIndex, endIndex);

Parameters:

  • startIndex: The index at which to begin extraction (inclusive).
  • endIndex: The index at which to end extraction (exclusive).

Example: Here, the slice() method extracts the array from the given array starting from index 2 and including all the elements less than index 4.

Javascript

function func() {
    // Original Array
    let arr = [23, 56, 87, 32, 75, 13];
    // Extracted array
    let new_arr = arr.slice(2, 4);
    console.log(arr);
    console.log(new_arr);
}
func();

Output

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



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Append Header to a HTML Table in JavaScript ? How to Append Header to a HTML Table in JavaScript ?
Add Characters to a String in JavaScript Add Characters to a String in JavaScript
Split a String into an Array of Words in JavaScript Split a String into an Array of Words in JavaScript
What is a Set in JavaScript? What is a Set in JavaScript?
Is set Ordered in JavaScript ? Is set Ordered in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14