+ 3

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

14th Feb 2020, 12:49 AM
Marina Khamis
Marina Khamis  - avatar
4 Respuestas
+ 5
It was pushed to the num array. But it got replaced by "Z".
14th Feb 2020, 1:03 AM
Kevin ★
+ 4
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
14th Feb 2020, 1:19 AM
ODLNT
ODLNT - avatar
+ 4
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.
16th Feb 2020, 7:12 PM
Kevin ★
+ 1
Kevin Star so why when i remove " Z " , it doesn't print " E " ?
16th Feb 2020, 11:35 AM
Marina Khamis
Marina Khamis  - avatar