Exercise

Loops are everywhere in programming, it’s important to practise writing them until they become second nature to you, so let’s spend some time writing a few loops and trying out the different ways of achieving our desired results.

  • Open up Google Chrome and head over to your console in inspector tools.
  • Create an array of your choosing with at least five values.
  • Write a loop to loop through the array and log to the console its values.
  • Write another loop to loop through the array in REVERSE order.
  • Write one final loop, which loops through your array, but is guaranteed to run at least once, regardless of the array’s length.

Make sure you get the desired outcome for each loop before moving on. Loops are a fundamental part of programming and it’s vitally important that you are fully comfortable with how they work.

What we have learned in this chapter

Before we move on to the next chapter, let’s recap exactly what we have just learned. We have taken a deep look into the power of arrays and understood exactly how they work along with their uses and functionality. We explored the built-in methods and values that come along with any array we create, before creating many of our own arrays and manipulating them at will. We have seen how to add, remove, change and sort our arrays to our requirements. We took a deep look at all of the methods we might want to use with our arrays before finally considering the differences between arrays and objects and their very similar nature, yet very different uses.

We have also covered loops. Loops are actually pretty simple, despite how powerful they are. While they can seem quite complex, there are just four of them, which all do pretty much the same thing, albeit in their own unique, slightly different way. They help to cover all use-cases of ways to loop through your data, and it is always better to have more ways to do something than none at all. Spend some time getting especially familiar with the standard for loop before moving on. The for loop will be the one you see the most out there in the wild. So there we have it, you understand loops! You’re one step closer to becoming a fully fledged web developer. Loops are a core piece of the JavaScript (and general programming) puzzle and they are used everywhere, so feel proud of yourself for getting to grips with them. They are powerful little things that will allow you to create all manner of interesting algorithms for manipulating your arrays and objects. Never underestimate the power of the loops. In the next chapter, we are going to explain a bit more about how the condition statements inside these loops work.

This chapter was probably the most difficult we have seen so far and certainly put our understanding of JavaScript to the test. To have made it this far already is an achievement, but don’t stop just yet. We are about to explore a huge part of programming that takes your arrays and makes use of them in a plethora of ways. We are about to see how much time programming can save us when working with data as we move on to the fourth and final part of the JavaScript section.