【问题标题】:Can you create a for loop with the conditionExpression is i == 0 in JavaScript?你能在 JavaScript 中用 conditionExpression is i == 0 创建一个 for 循环吗?
【发布时间】:2023-02-01 01:22:05
【问题描述】:

我想创建一个 for 循环以向后循环遍历数组。 我这样写代码:

for (let i = array.length; i == 0; i--){
   console.log('Test');
}

但是控制台中没有任何记录。 为什么?

顺便说一句,我也试过i === 0;

【问题讨论】:

  • i 设置为 array.length 时,它很可能不为零。条件必须被评估为 truthy,否则循环不会进入主体。

标签: javascript arrays loops for-loop


【解决方案1】:

你必须在 i > 0 时循环:

for (let i = array.length; i > 0; i--){
   console.log('Test');
}

【讨论】:

    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多