typestar

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

  1. for...of walks any iterable's values.
  2. continue skips an iteration; break exits.
  3. Object.entries destructures into key and value.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Control flow, step 11 of 43 in Language basics.

← Previous Next →