Array Slice and Splice Methods
Video
JavaScript Notes
JavaScript
// Array.prototype.slice([start[, end]])
// returns shallow copy of removed/sliced items
// Array.prototype.splice(start[, deleteCount[, item1[, item2[, ...]]]])
// modifies original array. returns array of deleted items
const log = console.log;
let names = ['Barney', 'Marshall', 'Lily', 'Robin', 'Ted'];
log(names.slice(3, 4)); //starts at 3, and upto but not including item at position 4
let res = names.splice(1, 0, 'Luke', 'Leia'); //starting at position 1, delete nil items, and add new items at position 1
log(res); // this will hold what was removed from the array - in the above example, nothing!
log(names); // this will have the 2 extra addional items
Feedback
Submit and view feedback