Arrays are objects (i.e. a special type of variable) that can hold multiple values . For example, if we had a list of items (e.g. a list of country names), storing each country name in single variables could become a hefty list to sort through when you're trying to find a specific country.
Arrays solve this problem, since an array can hold many values under a single name, and we can access the values by referring to an index number.
Note that array indexes start with 0 (i.e. [0] is the first element, [1] is the second element, and so on.
JavaScript comes with some handy built-in array methods to help you sort, search or edit an array, as well as much more! The following list describes some of the main array methods available, or check out w3schools complete JavaScript Array Reference .
Loops offer a quick and easy way to do something repeatedly. There are many different types of loops in JavaScript, which all essentially do the same thing: they repeat an action some number of times (including 0 times), based on a condition or set of conditions. The various loop mechanisms offer different ways to determine the start and end points of the loop, and there are various situations that are more easily served by one type of loop over the others.
Loops are useful when we want to run the same code repeatedly, each time with a different value . Some of the loops available in JavaScript are:
For now, we will only be focusing on for loops and while loops.
A for loop repeats until a specified condition evaluates to false :
When a for loop executes, the following occurs :
A while loop is the most general form of loop statements. A while statement executes its statements as long as a specified condition evaluates to true (i.e. while the condition is true), and stops executing when the condition becomes false, passing control to the statement following the loop . The while loop syntax is as follows:
The condition test happens before the statement (i.e., the code block in the loop) is executed. If the condition returns true, the statement is executed and the condition is tested again. If the condition returns false, the execution stops (i.e. it breaks out of the loop).
Beware the infinte loop!
Always avoid infinite loops by ensuring the condition in a loop eventually becomes false. If we do not do this, the loop will never terminate! The statements in this example while loop below would execute forever because the condition never becomes false:
This example demonstrates how we can use a for loop to swap numbers around.
The function in this example is called swapNumbers(). It prompts a user to input a number three times, and stores the inputted numbers in an array. It then iterates through the array backwards, storing each digit in reverse order in a new array, and outputs the new array (i.e. the reversed inputted numbers) to the <h5 id="arrayExample"> and the <h5 id="loopsExample"> elements in the arraysLoops.html file.
See an array and a for loop in action by clicking the button below. To see what's going on behind the scenes, right-click anywhere on the page and click inspect.