In this JavaScript, why don’t i get azbc?
var x = "a-b-c".split('-').splice(1, 0, 'z');
alert(x.join(''));
split returns an array containing a, b and c.
Shouldn’t splice insert z after a and gives me azbc?
Why do i get an empty array?
note:
i know that what i want can be accomplished by:
var x = "a-b-c".split('-')
x.splice(1, 0, 'z');
alert(x.join(''));
since splice “modifies” the original array itself. shouldn’t it modify {a,b,c} to {a,z,b,c} and then be assigned to x?
got it… the code below helped me to understand.
var x = "a-b-c".split('-')
x = x.splice(1, 0, 'z');
alert(x.join(''));
splicereturns the removed items from the array, not the new array: