Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
We've already learnt that functions are great for automating chunks of code. However, there's one downside to functions, and that is that you have to call them every single time you want to use them. For example, want to run a function 5 times? Then call it 5 times. Want to run it 100 times? Call it 100 times. As you can see, writing 100 lines of code to call the same function is a little pointless. It would seem a bit odd for JavaScript to let us down now after being so awesome with functions and arrays. If you're doing the same thing 100 times, surely there's an easier way to do it? The good news is that JavaScript has not let us down (it's too good for that), it has a perfect set of features to deal with this — loops.
As a really simple example, imagine if we had a list of names in an array and wanted to run our good friend the formatName function 5 times, once for each name. Without loops it would look something like this: