Loops and iteration in JavaScript
for...of, classic for, and iterating object entries.
const words = ["alpha", "beta", "gamma"];
for (const word of words) {
if (word === "beta") continue;
console.log(word);
}
for (let i = 0; i < 3; i++) {
console.log(i);
}
const scores = { ada: 95, grace: 88 };
for (const [name, score] of Object.entries(scores)) {
console.log(`${name}: ${score}`);
}
How it works
for...ofwalks any iterable's values.continueskips an iteration;breakexits.Object.entriesdestructures into key and value.
Keywords and builtins used here
Objectconstcontinueforifletof
The run, in numbers
- Lines
- 15
- Characters to type
- 301
- Tokens
- 104
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 73 seconds.
Step 1 of 3 in Control flow, step 11 of 43 in Language basics.