5

Looping with for, While, and Do…While!

In this Chapter

  • Learn how to have some code run repeatedly

  • Work with for, while, and dowhile loops

When we are coding something, there will be times when we want to repeat an action or run some code multiple times. For example, let’s say we have a function called saySomething that we want to repeatedly call 10 times.

One way we could do this is by simply calling the function 10 times using copy and paste:

saySomething();
saySomething();
saySomething();
saySomething();
saySomething();
saySomething();
saySomething();
saySomething();
saySomething();
saySomething();

This works and accomplishes what we set out to do, but…we shouldn’t do something like this. After all, duplicating code is never a good ...

Get Javascript Absolute Beginner's Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.