Confusion with javascript array.splice()

The "splice()" function returns not the affected array, but the array of removed elements. If you remove nothing, the result array is empty.


splice() modifies the source array and returns an array of the removed items. Since you didn't ask to remove any items, you get an empty array back. It modifies the original array to insert your new items. Did you look in a to see what it was? Look for your result in a.

var a = [1,2,3,4,5];
var b = a.splice(1, 0, 'foo');
console.log(a);   // [1,'foo',2,3,4,5]
console.log(b);   // []

In a derivation of your jsFiddle, see the result in a here: http://jsfiddle.net/jfriend00/9cHre/.