


Using the JavaScript slice method with only one argument- sentenceArray.slice(2)-we can create a new array containing all elements starting from index 2: result =. It takes two arguments, a start index and an end index, which determine which part of the array will be returned in the new one.īoth these indexes are completely optional, so if you leave them blank, they will default to 0 (the first element) and length (the last element).īy using this powerful method, you can easily retrieve part of an array or string, create substrings and arrays from existing ones, and even remove elements from a given array without mutating it.Īs an example, let's say we have an array called sentenceArray containing a sentence broken down into individual words:Ĭonst sentenceArray = The JavaScript slice method allows you to take out one or more books from the shelf without rearranging the remaining ones.

To start off with the basics, imagine a JavaScript array as a bunch of books on a shelf. It's one of those methods that, once you understand and can use, can make your developer life much easier! Put simply, the slice method is a powerful JavaScript tool that lets you extract sections of an array or string. If you're new to coding, the term 'slice method' may be daunting.
