var vacationSpots = ['Paris', 'New York', 'Barcelona'];
for(var i = vacationSpots.length - 1; i >= 0; i--) {
console.log('I would love to visit ' + vacationSpots[i]);
}
Hello guys,my question is whats the logic in "-1" in the for loop to be reverse.i get it that for(var i = vacationSpots.length; i >= 0; i--) { helps you running Backwards. but what's the use of -1 in printing the items in array backwards?
i=3, then it'll try to get the 4th item of the array, as arrays are 0-indexed. Since the length is 1-indexed (starts at 1), you have to do- 1to make sure it is in range of the array.