The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
1. Создадим массив из массива начиная с n позиции в массиве. Например, мы хотим начать использовать список с 3 элемента
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
Или, например, мы хотим взять последние 2 элемента из списка
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]
2. Клонирование массива. Иногда нужно получить копию массива без ссылок, просто используем slice без аргументов
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice());
// expected output: Array ['ant', 'bison', 'camel', 'duck', 'elephant']
Изменения в копии не приведут в изменении в основном массиве
3. Преобразования массиво-подобных объектов в массив
function argToArray(){
return Array.prototype.slice.call(arguments);
}
console.log(aargToArray("1", "2", "3", "4"));
// expected output: Array ["1", "2", "3", "4"]
4. Преобразовать NodeList из DOM в массив
const allDiv = document.querySelectorAll(‘div’);
console.log(Array.prototype.slice.call(allDiv));
expected output:

5. Заменить элемент в строке по индексу
String.prototype.append = function (index,value) {
return this.slice(0,index) + value + this.slice(index);
};
const person = "Captain Sparrow";
console.log(person.append(8,"Jack "));
expected output: String Captain Jack Sparrow
