O'Reilly logo

JavaScript Absolute Beginner’s Guide by Kirupa Chinnathambi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required