How to traverse an array in JavaScript

In JavaScript, an array is a data structure that stores a collection of elements (values or objects) of different data types, such as numbers, strings, booleans, objects, or even other arrays. The elements of an array are stored in a contiguous memory location, and each element is assigned a unique index (starting from zero) that represents its position in the array.

Syntax

To create an array in JavaScript, we can use the array literal syntax, which uses square brackets to enclose the elements of the array.

const array_name = [item1, item2,...];

Note: Arrays are often declared using the const keyword.

Array operations

In JavaScript, we can perform various operations on arrays, such as:

  • Accessing array elements

  • Modifying array elements

  • Removing array elements

  • Adding array elements

Array traversal methods

Instead of having to perform an action for each item in an array individually, loops allow us to repeat the action a selected number of times. There are many types of loops that can be utilized, but the two most common are the while loop and the for loop.

Using a while loop

In JavaScript, when using a while loop to iterate over an array, the loop executes repeatedly as long as the specified condition remains true. However, once the condition evaluates to false, the loop terminates, and control is passed to the next statement in the program.

const favNumbers = [7,21,25,99,404]; //declaring the array
let i = 0;
while (i < favNumbers.length)
// while loop that continues as long as i is less than the length of the array
{
console.log(favNumbers[i]); // prints out the element at the specified location in the array
i++; //add 1 to the increment
}

Explanation

  • Line 1: Declare an array with the keyword const.

  • Line 2: Declare our increment variable.

  • Line 4: Create a while loop that prints out each element at each spot in the array until the increment is as large as the array. The length of the array is obtained by using the length property.

  • Line 7: The loop prints out each element at the specified spot.

  • Line 8: Add one to the increment variable.

Using a for loop

In JavaScript, a for loop is a control flow statement that allows us to iterate over an array for a fixed number of times. When using a for loop to iterate over an array, the loop executes as long as the condition expression remains true. Inside the loop, we can access each element of the array using its index, which starts at 0 and increments by 1 on each iteration. Once the condition evaluates to false, the loop terminates, and control passes to the next statement in the program.

const favNumbers = [7,21,25,99,404]; //declaring the array
//In this order we can see the initialization, condition, and iteration
for (let i = 0; i < favNumbers.length; i++)
{
console.log(favNumbers[i]); //every time the loop runs, one spot in the array will be printed
}

Explanation

  • Line 1: Declare an array with the keyword const.

  • Line 4: Create the for loop that contains an initialization, condition, and iteration.

  • Line 6: Run the loop, so one spot in the array is printed. The loop then repeats with the increment being increased by 1.

Free Resources