Why doesn't array.splice() work when the array has only 1 element?

arrays, javascript, reduce

Solution

Because it returns what was removed, which is [1] in your case. `arr` will be empty after the call.

See example:

let arr = [1];
arr.splice(0, 1);
console.log(arr);

Problem

Is this intended behavior? I would expect an empty array to be returned here. JavaScript ``` let arr = [1]; console.log(arr.splice(0, 1)) ``` Console ``` 1 ```

Original source