This post was most recently updated on July 31st, 2024
Definition:
slice(): this array method is used to slice out specific portion of an array and returns new array. or We can use this method to get specific part and remove remaining part of an array. We can use two arguments in this method like slice(1,3).
When we use two arguments in this method, array selection starts from the first argument and up to the end argument but returns new array excluding end argument.
See the example code below:
1 2 3 4 5 6 7 8 |
//JavaScript: array.shift() var myArray = ['Steve', 'James', 'Ombala', 'Marksen', 'Jackson']; var sliceArray = myArray.slice(1); //expected output after slice array is: console.log(sliceArray); // ['James', 'Ombala', 'Marksen', 'Jackson'] var sliceArray1 = myArray.slice(1,3); //expected output after slice array is: console.log(sliceArray1); // ['James', 'Ombala'] |