5. Looping with for, While, and Do...While!
In This Chapter
• Learn how to have some code run repeatedly
• Work with
for
,while
, anddo...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...we shouldn’t do something like this. After ...
Get JavaScript Absolute Beginner's Guide, 2nd 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.