How many times can you
Let’s start with the definition of
The first parameter is called
startand represents the index of the first element that will be removed from the array.
The second argument is
deleteCount. It determines the number of array elements that will be removed from the array
The third, the fourth argument, and so on, are the new elements that will be added to the array.
splicereturns the array formed by deleted elements.
Now, we start the array
arr with 5 elements
[1, 2, 3, 4, 5].
splice extracts 2 elements starting from
arr. We immediately save them into the
Before the final splice we have the following state:
[ 1, 4, 5 ] // arr [ 2, 3 ] // splicedArr
splice once again removes 2 elements from
arr starting at
arr. This leaves us with a single element —
Then, we apply the destructuring with
... to the
splicedArr and add elements
3 to the initial array
Here’s the code snippet with 2 additional calls to
console.log to help you understand the explanation better:
const arr = [1, 2, 3, 4, 5]; const splicedArr = arr.splice(1, 2); console.log(arr); // [ 1, 4, 5 ] console.log(splicedArr); // [ 2, 3 ] arr.splice(1, 2, ...splicedArr); console.log(arr);
ANSWER: the array will eventually hold values
[ 1, 2, 3 ] which will be logged to the console.