Javascript 为什么输出不是逻辑?

Javascript 为什么输出不是逻辑?,javascript,for-loop,Javascript,For Loop,let vacationSpots=[“美国”、“英国”、“哥伦比亚”]; 对于(让vacationSpotIndex=vacationSpots.length-1;vacationSpotIndex>=0;vacationSpotIndex--){ console.log('我很想访问'+vacationSpots[vacationSpotIndex]); }当您使用>=0时,它将继续循环,当它为0时,将导致超出范围(-1),使其正好>0 let vacationSpots = ['USA',

let vacationSpots=[“美国”、“英国”、“哥伦比亚”];
对于(让vacationSpotIndex=vacationSpots.length-1;vacationSpotIndex>=0;vacationSpotIndex--){
console.log('我很想访问'+vacationSpots[vacationSpotIndex]);

}
当您使用>=0时,它将继续循环,当它为0时,将导致超出范围(-1),使其正好>0

let vacationSpots = ['USA', 'UK', 'Colombia']; 
 for (let vacationSpotIndex = vacationSpots.length - 1; vacationSpotIndex > 0; vacationSpotIndex-- ) {
   console.log('I would love to visit '  +        vacationSpots[vacationSpotIndex]);
  }
数组索引在javaScript中从0开始

一切正常。这里没有什么问题

let vacationSpots = ['USA', 'UK', 'Colombia']; 
    vacationSpots.length=3
    index->0='USA'
    index->1='UK'
    index-2='Colombia'
for(让vacationSpotIndex=vacationSpots.length-1;vacationSpotIndex>=0;vacationSpotIndex--){

vacationSpotIndex>=0;
此条件表示迭代循环,直到vacationSpotIndex大于或等于零,并在每次迭代之前进行检查。

循环将从索引=2开始,直到索引=0

vacationSpotIndex=2
vacationSpotIndex=1
vacationSpotIndex=0

我可能错了,但你的问题似乎是,为什么不是
vacationSpotIndex>=0
而是
vacationSpotIndex?预期的输出是什么?你到底在问什么?请花几分钟的时间来帮助我们阅读。你真的没有解释你的问题,或者你到底希望这段代码做什么与它当前的功能相比,“我假设逻辑上它是
vacationSpotIndex,他的条件是检查一个没有数组的整数。因此,没有索引。仅仅因为它的名称为“index”,并不意味着它是数组的索引。它是一个整数。它将在=0然后0-1然后vacationSpots[-1]时循环答案毫无意义你的答案和我的有什么不同answer@Osama你的代码省略了数组的第一项。OP的代码是正确的(我想,这个问题不是很清楚)。我以为OP只是想解释它为什么工作,这就是我的答案。