for 循环的增量/减量可以超过 1 吗? [英] Can a for loop increment/decrement by more than one?

查看:27
本文介绍了for 循环的增量/减量可以超过 1 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 i++++i 之外,还有其他方法可以在 Javascript 中增加 for 循环吗?例如,我想增加 3 而不是 1.

Are there other ways to increment a for loop in Javascript besides i++ and ++i? For example, I want to increment by 3 instead of one.

for (var i = 0; i < myVar.length; i+3) {
   //every three
}

推荐答案

使用 += 赋值运算符:

for (var i = 0; i < myVar.length; i += 3) {

从技术上讲,您可以在 for 循环的最终表达式中放置您想要的任何表达式,但它通常用于更新计数器变量.

Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.

有关 for 循环每一步的更多信息,检查出 MDN 文章.

For more information about each step of the for loop, check out the MDN article.

这篇关于for 循环的增量/减量可以超过 1 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆