What is the purpose of the array slice method
The slice() method in JavaScript is used to extract a section of an array, returning a new array containing the selected elements. It does not modify the original array. The method takes two arguments:
start: The index at which extraction begins (inclusive).
end (optional): The index before which to end extraction (exclusive). If omitted, extraction continues to the end of the array.
You can also use negative indices, which count from the end of the array.
Examples:
let arrayIntegers = [1, 2, 3, 4, 5];
let arrayIntegers1 = arrayIntegers.slice(0, 2); // [1, 2]
let arrayIntegers2 = arrayIntegers.slice(2, 3); // [3]
let arrayIntegers3 = arrayIntegers.slice(4); // [5]
let arrayIntegers4 = arrayIntegers.slice(-3, -1); // [3, 4]
Note:
The slice() method does not mutate (change) the original array; instead, it returns a new array containing the extracted elements.
What is the purpose of the array splice method
The splice() method in JavaScript is used to add, remove, or replace elements within an array. Unlike slice(), which creates a shallow copy and does not alter the original array, splice() modifies the original array in place and returns an array containing the removed elements.
Syntax
array.splice(start, deleteCount, item1, item2, …)
start: The index at which to start changing the array.
deleteCount: (Optional) The number of elements to remove from the array. If omitted, all elements from the start index to the end of the array will be removed.
item1, item2, …: (Optional) Elements to add to the array, starting at the start position.
Examples
let arrayIntegersOriginal1 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal2 = [1, 2, 3, 4, 5];
let arrayIntegersOriginal3 = [1, 2, 3, 4, 5];
// Remove the first two elements
let arrayIntegers1 = arrayIntegersOriginal1.splice(0, 2);
// arrayIntegers1: [1, 2]
// arrayIntegersOriginal1 (after): [3, 4, 5]
// Remove all elements from index 3 onwards
let arrayIntegers2 = arrayIntegersOriginal2.splice(3);
// arrayIntegers2: [4, 5]
// arrayIntegersOriginal2 (after): [1, 2, 3]
// Remove 1 element at index 3, then insert “a”, “b”, “c” at that position
let arrayIntegers3 = arrayIntegersOriginal3.splice(3, 1, “a”, “b”, “c”);
// arrayIntegers3: [4]
// arrayIntegersOriginal3 (after): [1, 2, 3, “a”, “b”, “c”, 5]
Note:
The splice() method modifies the original array.
It returns an array containing the elements that were removed (if any).
You can use it both to remove and insert elements in a single operation.
What is the difference between slice and splice
Here are the key differences between slice() and splice() methods in JavaScript arrays:
slice() splice()
Does not modify the original array (immutable) Modifies the original array (mutable)
Returns a shallow copy (subset) of selected elements Returns an array of the removed elements
Used to extract elements from an array Used to add, remove, or replace elements in an array
Syntax: array.slice(start, end) Syntax: array.splice(start, deleteCount, …items)
Summary:
Use slice() when you want to copy or extract elements without altering the original array.
Use splice() when you need to add, remove, or replace elements and want to change the original array.