JavaScript Array Slice() Method with Example

JavaScript Array.slice() method slices out or extracts a sequence out of an array. It returns a slice, or sub-array, of the specified array.

Using slice() method, we can copy a portion of an array and assign it to a new variable name.

The slice() method works on the copy of an array. It does not modify the original array on which it is called.

JavaScript slice() Method Syntax

The slice() method of Array object comes into two flavors that are as:

array.slice(start, end);

The slice() method returns a shallow copy of elements from the original array. A new array that contains the sliced out or extracted elements.

slice() Method Parameters

If only one parameter (index value) is specified, the slice() method creates a sequence of elements that begins from index and ends on the last array element.

In other words, the returned array contains all elements from the starting to ending of the array.

If both start and end indexes are specified, the slice() method creates a sequence of elements that begins from the start index up till the end index – 1.

In other words, the returned array contains the element specified by the first parameter start and all subsequent elements up to, but not including element the specified by the second parameter end.

If either argument start or end is a negative value, it specifies an array element relative to the last element in the array.

JavaScript Array slice() Method Example

1. Suppose we have an array of names shown in the following table:

Index   0    1    2  3   4

Suppose we want to create a new array with elements 1, Ivaan, and 2, Adam. We would call slice() method and specify a start index of 1 and an end index of 3. The code would be something like this:

Program code 1:

let names = ["Paul", "Ivaan", "Adam", "Bob", "Deep"]; 
let slicedArray = names.slice(1, 3);
document.write("Original array: " +names, "<br>");
document.write("Sliced array: " +slicedArray);
      Original array: Paul,Ivaan,Adam,Bob,Deep (Original array unaffected)
      Sliced array: Ivaan,Adam

When JavaScript copies a portion of an array, it copies the new elements to an array in which indexes start from 0, 1, 2…. JavaScript does not use their old indexes to place their new elements in the array.

After slicing out elements of an array, the slicedArray looks like the following table:

Index   0    1

As you observe in the output, the first array, “names” is unaffected by slicing.

2. Let’s create a JavaScript program to slice out a portion of elements of an array with different indexes. Look at the following script code to understand better.

Program code 2:

let languages = ["Python", "JavaScript", "C", "C++", "Java", "Go"];

// Slicing the array elements (from start to end).
  let slicedArray = languages.slice();
  document.write(slicedArray, "<br>");

// Slicing from the second element in the array.
  let slicedArray1 = languages.slice(1);
  document.write(slicedArray1, "<br>");

// Slicing from the second element to fifth element.
  let slicedArray2 = languages.slice(1, 5);

JavaScript slice() With Negative index

In the JavaScript slice() method, we can also use negative start and end indices. The index of the last element will be -1, the index of the second last element will be -2, and so on. Let’s take an example program based on it.

Program code 3:

  const colors = ['Red', 'Yellow', 'Green', 'Black', 'White'];
// Slicing array elements from start to third.
  let slicedArray = colors.slice(2);
  document.write(slicedArray, "<br>");

// Slicing array elements from second to fourth
  document.write(colors.slice(1, 4), "<br>");

// Slicing the array elements from start to second last.
  document.write(colors.slice(0, -1), "<br>");

// Slicing the array elements from the third last.

JavaScript slice() Method with Objects as Array Elements

Array.slice() method does not alter the original array. It returns a shallow copy of elements from the original array.

JavaScript copies elements of the original array into the returned array in the following ways:

1. For objects, Array.slice() method copies object references into the new array. Both the original and new array refer to the same object.

Therefore, if we modify or update an object, the changes are visible to both new and original arrays.

2. For booleans, strings, and numbers (not Boolean, String, and Number objects), Array.slice() method copies values into the new array.

Changes to the boolean, string, or number in the new array do not affect the original array. If we add a new element to either array, the other array is not affected.

Let’s create a JavaScript program in which we will make a change to the object in the new array and see changes are visible in the returned new array and original array or not. Look at the following script code.

Program code 4:

let person = {
    name: "Bob",
    age: 23,
let originalArray = [person, "Dhanbad", "Student"];
let newArray = originalArray.slice();

// Original object.

// Making a change to the object in new array.
   newArray[0].name = "David";

// Changes are visible in the original array.
      { name: 'Bob', age: 23 }
      { name: 'David', age: 23 }

Browser Support

Copying a part of an array using slice() method is a significant feature of ECMAScript1 (ES1). The following browsers support fully ES1 (JavaScript 1997).

  • Chrome
  • IE
  • Edge
  • Firefox
  • Safari
  • Opera

In this tutorial, you learned JavaScript Array slice() method with example programs. Hope that you will have understood the basic concept of Array.slice() in JavaScript and practiced all programs.
Thanks for reading!!!
Next ⇒ JavaScript array fill() method⇐ Prev Next ⇒

Please share your love