Published on

Cycle through array/list using modulo operator

Code to (indefinitely) cycle through (and print) the items of a list

function main(startAt = 0) {
let counter = startAt;
const list = ["a", "b", "c", "d", "e"];
setInterval(() => {
const index = counter % list.length;
console.log(list[index]);
counter++;
}, 1000);
}
main();