JAVASCRIPT · ОСНОВЫ · LESSON 14

Each element

for...of loop · about 12 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Go round the list

for...of takes each value from the array in turn.

const nums = [2, 4];
for (const number of nums) {
  console.log(number);
}

✦ 2 and 4 will appear.

STEP 2

Read as a sentence.

for (Zconst number of nums) stands for «для каждого number из nums».

const pets = ["кот", "пёс"];
for (const pet of pets) {
  console.log(pet);
}

✦ Each pet will appear on a new line.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the names, brackets and signs.

const nums = [2, 4];
for (const number of nums) {
  console.log(number);
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create const coins as an array 1, 3, 5. With for...of, output each number; the current element is called coin.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework