JavaScript Array slice() Method

Javascript array slice() method extracts a section of an array and returns a new array.

The JavaScript array slice() method extracts the part of the given array and returns it. This method doesn't change the original array.


array.slice(start, 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.


var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var myBest = fruits.slice(-3, -1);

Example -