Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript Array slice()

The slice() method extracts a segment of an array, creating a new array with those elements.

Example

Extract a portion of an array, commencing from array element 1 (“Orange”):

const fruits = [“Banana”“Orange”“Lemon”“Apple”“Mango”];
const citrus = fruits.slice(1);

Please note that the slice() method generates a new array.

Additionally, the slice() method does not eliminate any elements from the source array.

Example

Extract a portion of an array, beginning from array element 3 (“Apple”):

const fruits = [“Banana”“Orange”“Lemon”“Apple”“Mango”];
const citrus = fruits.slice(3);

The slice() method can accept two arguments, such as slice(1, 3).

In this scenario, the method selects elements from the starting argument up to (but not including) the end argument.

Example

const fruits = [“Banana”“Orange”“Lemon”“Apple”“Mango”];
const citrus = fruits.slice(13);

When the end argument is omitted, as in the initial examples, the slice() method extracts the remaining portion of the array.

Example

const fruits = [“Banana”“Orange”“Lemon”“Apple”“Mango”];
const citrus = fruits.slice(2);