
`end` is the index number of the item that you want to exclude from the new array. If you don’t specify a start parameter, the slice will start from the beginning of the array (index 0).
`start` is the index number of the first item that you want to include in the new array. Here is the syntax for using the slice() method: array.slice(start, end) The index of the first fruit “apple” is 0, the index of “banana” is 1, the index of “orange” is 2, and so on. The index of the first item in the array is always 0, the second item is 1, and so on.įor example, let’s say we have an array of fruits: let fruits = To understand and use the slice() method effectively, it’s helpful to have a basic understanding of how arrays work and how array indexes are used.Īn array is a collection of items that are stored in a specific order, and each item in the array is assigned an index number that represents its position in the array. It only creates a new array with the items you took out. Just like you didn’t change the big box when you took out balls, the slice() method doesn’t change the original array when you take out items. So, you use the slice() method to take out the items you want and put them in a new array. You want to take out some items and put them in a new array. You have a big array with lots of items inside.
The slice() method works the same way with arrays. So, you take out a few balls from the big box and put them in the smaller box. Now, you want to take out some balls and put them in a smaller box. Imagine you have a big box with lots of balls inside. In this blog we will be learning what is slice() method and how to use it in javascript and javascript based frameworks like react, angular, vue, node.js and more. There are lots of Array methods in javascript and slice() is one of them.