JavaScript Array slice()

Posted by

Method

Introduction to JavaScript Array slice() Method

The JavaScript array slice() method is used to extract a part of an array, creating a new array object containing the extracted elements. The original array is not affected by the slice() method.

Syntax

The syntax for the JavaScript array slice() method is as follows:

array.slice(start, end);

Parameters

The slice() method takes two parameters:

  • start – The index at which to begin the slice. If it is a negative number, it is taken as the offset from the end of the array.
  • end – The index at which to end the slice. If it is a negative number, it is taken as the offset from the end of the array. If it is omitted, the slice continues until the end of the array.

Examples

Let’s look at some examples of using the JavaScript array slice() method.

Example 1: Basic use of the array slice() method

In this example, we will create an array of numbers and then use the slice() method to extract a portion of the array.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Create an array of numbers
var numbers = [1,2,3,4,5,6,7,8,9];

// Slice the array
var sliced = numbers.slice(3,7);

// Log the result
console.log(sliced); // [4,5,6,7]

[/dm_code_snippet]

In this example, we first create an array of numbers. Then, we use the slice() method to extract elements 3 through 7 from the array. When we log the result, we see that it is a new array containing the elements 4, 5, 6, and 7.

Example 2: Using negative numbers with the array slice() method

In this example, we will use negative numbers as the parameters for the slice() method.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Create an array of numbers
var numbers = [1,2,3,4,5,6,7,8,9];

// Slice the array
var sliced = numbers.slice(-3,-1);

// Log the result
console.log(sliced); // [7,8]

[/dm_code_snippet]

In this example, we again create an array of numbers. Then, we use the slice() method to extract elements 3 from the end through 1 from the end of the array. When we log the result, we see that it is a new array containing the elements 7 and 8.

Example 3: Omitting the end parameter with the array slice() method

In this example, we will omit the end parameter when using the slice() method.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Create an array of numbers
var numbers = [1,2,3,4,5,6,7,8,9];

// Slice the array
var sliced = numbers.slice(4);

// Log the result
console.log(sliced); // [5,6,7,8,9]

[/dm_code_snippet]

In this example, we again create an array of numbers. Then, we use the slice() method to extract elements 4 and beyond from the array. When we log the result, we see that it is a new array containing the elements 5, 6, 7, 8, and 9.

Conclusion

The JavaScript array slice() method is a useful way to extract a portion of an array, creating a new array object containing the extracted elements. The original array is not affected by the slice() method. It takes two parameters: a start index and an end index. If the end index is omitted, the slice continues until the end of the array. Negative numbers can be used as parameters, in which case they are taken as offsets from the end of the array.