why wasn't "E" pushed to the num array ? | SoloLearn: Learn to code for FREE!


why wasn't "E" pushed to the num array ?

var num = ["A", "B", "C"]; var special= num.splice(num.push("D"), num.push("E"), "Z"); document.write(num); //prints A,B,C,D,Z

2/14/2020 12:49:13 AM

Marina Khamis

4 Answers

New Answer


It was pushed to the num array. But it got replaced by "Z".


Marina "E" was pushed to the array, but since you are using the array push method as parameters for the array splice method, "E" was cut from the array. var special = num.splice(num.push("D"), num.push("E"), "Z"); starting at index 4 remove 5 position and replace with an "Z" Splice - returns new Array, containing the removed items (if any)-w3schools.com document.write(special);//prints E


Marina If you do not specify any elements to add, splice() will only remove elements from the array. Read more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice [Edit a few months later]: I try to understand this answer now but I can't?? Downvoted myself.


Kevin Star so why when i remove " Z " , it doesn't print " E " ?