I think it should alert 4 times, but why it just alerted just 2 times. Is there anyone can make an explanation for me?
var arr = new Array;
arr.push("1");
arr.push("2");
arr.push("3");
arr.push("4");
for(var i=0;i<arr.length;i++){
alert(arr.pop());
}
Change your
forloop to awhileloop:The problem is that you remove elements from the array, so the
lengthdecreases, ending yourforloop early. After 2 iterations of yourforloop,arr.lengthis 2, andiis also 2, so the loop ends.Here’s a working example of the above.