Learn
Loops
Looping through Arrays
For Loop: One of the most common ways to loop through an array is by using a for loop. The for loop allows us to define a variable that will act as the index, the condition for the loop to continue, and the statement to increment the index.
For...of Loop:
Another way to iterate through an array is by using the for...of loop. This loop simplifies the code by directly accessing each element of the array without the need for an index.
The for...of loop is particularly useful when you only need to access the elements of the array and don't require the index.
Example
Let's consider an example to illustrate how to loop through an array. Suppose we have an array of numbers:
We can loop through this array using a for loop and log each element to the console:
This code will output:
Similarly, we can achieve the same result using a for...of loop:
The output will be the same as before.
Instructions
Replace the existing TODO
comment, write a for
loop that iterates through the whole numbers
array.
Within the loop, log each number to the console.
Sign up to start coding
Already have an account?
Sign In