Messing around at the end of the Array Review

I revisited the Array topic in part 5 of the JS module after being stuck during the Luhn Credit Card challenge and encountered the ‘issue’ on line 9 of my code. When the program is ran: it outputs “[ ]” onto the console instead of ‘Yup’ and yet when i call on the array again on line 10, the item on that index is now actually ‘Yup’. I’d like to know why this is, thank you in advance for your time!

const newArr = [1,2,3,4,‘hi’];
console.log(newArr.length);

var array = [1,2,3,4,5,6];
console.log(newArr.slice(0)[4]);
var val = newArr[newArr.length - 1];
console.log(val);
console.log(newArr.length);
console.log(newArr.splice((newArr.length - 1), 0, ‘Yup’)); //output is [ ]??? Why though?
console.log(newArr.join(’, '));

What is the return value of the Array.splice() method?

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Return value

An array containing the deleted elements.

If only one element is removed, an array of one element is returned.

If no elements are removed, an empty array is returned.

2 Likes

Super interesting! Thank you!

2 Likes