Open
Description
const numbers = [1, 2, 3, 4, 5, 6]
numbers.splice(3, 3, 7, 8, 9)
console.log(numbers.splice(3, 3, 7, 8, 9)) // -> [1, 2, 3, 7, 8, 9] //it removes three item and replace three items
small mistake - in case anyone who copy-pasted the code, got [7, 8, 9] instead of [1, 2, 3, 7, 8, 9] and started questioning life,
this is because the author called the splice()
method twice, and splice()
would modify the original array.
Corrected version:
const numbers = [1, 2, 3, 4, 5, 6]
console.log(numbers.splice(3, 3, 7, 8, 9)) // [4, 5, 6] - returns the removed items
console.log(numbers) // [1, 2, 3, 7, 8, 9] - returns current the array numbers
Metadata
Metadata
Assignees
Labels
No labels