本文主要是介绍JavaScript - Blocks - 循环,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
For 循环
for (initializer; exit-condition; final-expression) {// code to run
}
const cats = ['Bill', 'Jeff', 'Pete', 'Biggles', 'Jasmin'];
let info = 'My cats are called ';
const para = document.querySelector('p');for (let i = 0; i < cats.length; i++) {info += cats[i] + ', ';
}para.textContent = info;for (let i = 0; i < cats.length; i++) {if (i === cats.length - 1) {info += 'and ' + cats[i] + '.';} else {info += cats[i] + ', ';}
}
使用 break 跳出循环
跳出后,不再执行循环。
<label for="search">Search by contact name: </label>
<input id="search" typ
这篇关于JavaScript - Blocks - 循环的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!