Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Counting With the Classic for Loop

Counting With the Classic for Loop

In this lesson, you ported the night-bus report’s timetable loop from Python to JavaScript.

  • JavaScript has no range(). You count with the three-part for loop. Python’s range(10, 60, 20) becomes a start (let minute = 10), a condition (minute < 60) and a step (minute += 20), separated by semicolons inside the header’s parentheses.
  • The header runs in order: the start runs once, then the condition is checked, the body runs, the step runs, and the condition is checked again, until it’s false. In Python, that’s a while loop with the step as the body’s last line, though range() stays the way to write it there.
  • The rule from for...of still holds: const unless something reassigns the name. In a for...of loop, nothing does, because each pass gets a fresh variable. In a counting loop, the step reassigns the counter on every pass, so the counter is a let. A const counter stops the loop with a TypeError as soon as the first step runs.
  • The counter exists only inside the loop. Python’s loop variable keeps its last value after the loop, but in JavaScript, reading the counter after the closing brace is a ReferenceError.
  • To count down, start high, flip the condition, and subtract in the step. range() reads the direction from the step’s sign, but the condition is a plain test, so it has to point the way you count.
  • To count by one, the step is i++, which adds one to i, like i += 1.
  • For an array’s items rather than numbers, for...of is still the loop to write.

The finished timetable loop:

Language: JavaScript
for (let minute = 10; minute < 60; minute += 20) {
  console.log(`N26 leaves at 23:${minute}`);
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll port a loop that counts, and see how JavaScript counts without a range() function. The night-bus report matches each trip to its scheduled departure.

00:12 Route N26 runs every twenty minutes, so the Python script works the times out with range(): start at 10, stop before 60, and step by 20. Run it, and Python prints the last three departures before midnight.

00:29 JavaScript has no range() at all, so this loop needs a new shape.

00:35 Open the JavaScript version. The loop’s first line is its header. Parentheses wrap it, as in a for...of loop, and semicolons split it into three parts.

00:47 The first sets up the counter: minute starts at 10. The second is the condition to keep going: minute is less than 60. The third, the step, adds 20 with +=, as in Python.

01:02 The body goes in braces, and a template literal does the f-string’s job. In the report’s first loop, delay took three values, and const was fine, because each pass got a fresh one.

01:15 minute takes three values too, so you write const. What does Node print? Your turn: pause the video and predict.

01:28 Node prints one departure, then stops: TypeError, assignment to constant variable. JavaScript files this under TypeError, and the message says what happened.

01:40 The caret points at the step.

01:44 Follow the header in the order it runs. The start runs once. Then Node checks the condition: 10 is less than 60. The body runs and prints 23:10. Then the step reassigns minute, and a const can’t be reassigned.

02:03 Had the step worked, the loop would go round again, until the condition is false. In Python terms, the header means this while loop. The start comes first, the condition heads the loop, and the step is the body’s last line.

02:19 In Python, range() is still the way to write it. So it’s the same rule as before: const unless something reassigns the name, and here the step does, on every pass.

02:32 With let, Node prints the same three departures as Python.

02:38 Python’s loop variable keeps its last value after the loop. Add a quick check that prints minute after the loop, and Python prints 50.

02:49 Port the check to JavaScript, below the closing brace. What does it print? Your turn: pause the video and predict.

03:02 The three departures appear, and then Node stops: ReferenceError, minute is not defined.

03:10 That’s where the while picture stops: a let in the header belongs to the loop. After the closing brace, the counter is gone.

03:20 The departures board lists the latest first. In Python, the loop counts down with range(50, 0, -20). In JavaScript, the counter starts at 50, and the step subtracts 20.

03:36 The condition flips: keep going while minute is greater than 0.

03:42 range() reads the direction from its step, but a condition is only a test, so it has to point the way you count. Node prints them latest first, as Python does.

03:55 You’ve ported the report’s counting loops. So here’s the rule: count with the three-part for loop, start, keep going while, then step. To count by one, the step is i++, and the plus-plus adds one to the counter, the same as i += 1 in Python.

04:14 The step reassigns the counter, so it’s a let, and it lives only inside the loop. For an array’s items, for...of is still the loop to reach for, as in Python.

04:28 Next up is an exercise, Number the Stops, where you’ll write counting loops of your own.

Become a Member to join the conversation.