iterate over an array in javascript

In JavaScript, you can iterate over an array using different types of loops like for, while, or do-while. Here is an example using a for loop:

index.tsx
let array = [1, 2, 3, 4, 5];

for(let i = 0; i < array.length; i++){
  console.log(array[i]);
}
96 chars
6 lines

In the above example, we first create an array and then iterate through it using a for loop. The loop syntax is something like this: for(initialization; condition; increment/decrement){}.

The for loop initializes the counter variable i to 0. It then checks whether i < array.length is true. If it is true, the loop body executes, which in our case logs the current array element to the console.

After the loop body executes, the increment statement i++ updates the counter to 1. It continues this process until the condition i < array.length is false.

This is just one example of how to iterate over an array in JavaScript. There are many other ways to do this, but this should give you a good starting point for understanding the process.

gistlibby LogSnag