JAVASCRIPT Tutorial

Looping Through an Array

Key Concepts

  • Array: An ordered collection of elements.
  • Array Iteration: The process of visiting each element in an array.
  • Looping: A control structure that repeatedly executes a block of code.
  • Array Elements: The individual values stored in an array.

Practical Steps

  1. Define the Array: Declare and initialize an array with the desired elements.
  2. Use a for Loop: Use a for loop to iterate through the array.
  3. Access Array Elements: Use the loop variable as an index to access each element in the array.
  4. Perform Operations: Within the loop, perform any desired operations on the array elements.

JavaScript Example

// Define an array
const numbers = [1, 2, 3, 4, 5];

// Iterate through the array using a for loop
for (let i = 0; i < numbers.length; i++) {
    // Access and print the current element
    console.log(numbers[i]);
}

Output

1
2
3
4
5