Splice() Method - Confusing

I have hit a strange wall. The splice() method is behaving differently from what was explained.

Please take a moment to read the code below.
Console.log(newArray) is only returning one value and I cannot figure out why.
Console.log(newArray2) is returning two values, the first value is the correct ZBI(zero base index), however the second value is not.

May you please explain why, (newArray) is returning only a single value and why (newArray2) is also splicing at the wrong ZBI?

let array = ['I', 'am', 'feeling', 'really', 'happy'];
let array2 = ['Merry', 'Xmas', 'Everyone', 'And', 'Happy--']

let newArray = array.splice(3, 1);
let newArray2 = array2.splice(3, 2);

console.log(newArray);
console.log(newArray2);

Hi @P.Mat3 !

If you look at the docs it says this:
The return value is an array containing the deleted elements.

Your code here

is telling the computer to remove one element at index 3.

It is returning two values because that is what the second argument in splice is doing.
Your code here

tells the computer to remove two elements from the array starting at index 3.

Hopefully that clears it up.

1 Like

Thank you for clarifying.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.