I wanted to delete the first and third elements of the array using the forEach method. I was able to do this, but the question came to me: Why are elements deleted in forEach registered in the console but elements that were not deleted are not registered in the console?
Does not the forEach method apply to all elements of a functional array? So why aren't all the array elements registered in the console?
const cartItems = ['Book1', 'Book2', 'Book3', 'Book4']
cartItems.forEach(function (item, index, array) {
// I mean console.log below 👇
console.log(item)
if (index == 0) {
array.shift()
}
if (index == 1) {
array.splice(1, 1)
}
})
console.log(cartItems)
shift()andsplice()mutate the original array.