Looping backwards through javascript array with array.length using a for-loop
问题 If I have an array = [8,7,6,5,4] that I want to loop through, why does the following for loop still work yet the length of the array is 5 and there is no element at index 5 of the array? for(let i=array.length;i>=0;i++){ //do something } I know that it would be more accurate to subtract 1 from the length, but why does the code above still work 回答1: Almost. You have to: Decrease i instead of increasing it, and Start from array.length-1 , because array indexes start from 0, not 1. So use