DEV Community

Cover image for JavaScript Array.Prototype.slice() Method Explained
Kuldeep Singh
Kuldeep Singh

Posted on • Updated on • Originally published at programmingeeksclub.com

JavaScript Array.Prototype.slice() Method Explained

JavaScript’s Array slice() method returns a copy of array as new array, For returning the new array it accepts parameters which are optional as well, start which is inclusive and an end which is exclusive which means if you have passed 1 as start(index) and 3 as end(index) then slice will return a new copy of your given array which will contain two values(indexes). The slice() method does not change the original array. end is treated as end-1 internally in slice() method of array.

const cars = ['Toyota', 'Volkswagen','Daimler','Ford Motor','Honda','BMW','Hyundai','Nisaan','Renault','Kia'];

// no value
console.log(cars.slice());
// expected output: [ 'Toyota', 'Volkswagen', 'Daimler', 'Ford Motor', 'Honda', 'BMW', 'Hyundai', 'Nisaan', 'Renault', 'Kia' ]

// single index
console.log(cars.slice(4));
// expected output: [ 'Honda', 'BMW', 'Hyundai', 'Nisaan', 'Renault', 'Kia' ]

// from middle index
console.log(cars.slice(4,7));
// expected output: [ 'Honda', 'BMW', 'Hyundai' ]

// from beginning of the index
console.log(cars.slice(0,4));
// expected output: [ 'Toyota', 'Volkswagen', 'Daimler', 'Ford Motor' ]

// negative index
console.log(cars.slice(-4));
// expected output: [ 'Hyundai', 'Nisaan', 'Renault', 'Kia' ]

// from index 6 to -3(excluded)
console.log(cars.slice(4,-1));
// expected output: [ 'Honda', 'BMW', 'Hyundai', 'Nisaan', 'Renault' ]
Enter fullscreen mode Exit fullscreen mode

JavaScript Array.Prototype.slice() Syntax

slice()
slice(start)
slice(start, end)
Enter fullscreen mode Exit fullscreen mode

As you can see you can use slice() method in multiple ways it depends on you how you want to use because as you have seen in above code you can use negative indexes as well to access the values of array to create your own new array as of needed.

JavaScript Array.Prototype.slice() Parameters

start (optional)

Zero-based index at which to start extraction.

Negative index can be used, and it selects from the end of the array. slice(-3) extracts the last three elements in the sequance.

If start is omitted or undefined, slice starts from the index 0.

if start is greater than the length of the sequence(array), an empty array will return.

end (optional)

The index of the first element to exclude from the returned array. slice extracts up to but not including end which means end-1.

A negative index can be used, indicating an offset from the end of the sequence.

If end is omitted or undefined, slice extracts through the end of the sequence array.length.

If end is greater than the length of the sequence, slice extracts through to the end of the sequence array.length.

Return value of slice() function

Array method slice() returns a new array containing the extracted elements based on provided parameters.

This article is originally posted on programmingeeksclub.com

My Personal Blogging Website : Programming Geeks Club
My Facebook Page : Programming Geeks Club
My Telegram Channel : Programming Geeks Club
My Twitter Account : Kuldeep Singh

Top comments (0)