5. Meet the Loops: For, While, and Do...While!

In This Chapter

Learn how to have some code run repeatedly

Work with for, while, and do...while loops

When you are coding something, there will be times when you 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 you shouldn’t do something like this. Duplicating code is never a good idea.

Get JavaScript Absolute Beginner’s Guide 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.