loop a range in javascript

In JavaScript, you can use the for loop to iterate over a range of numbers. To create a range, you can use the start, stop, and step parameters of the for loop.

Here's an example of how to loop over the numbers from 1 to 10:

index.tsx
for (let i = 1; i <= 10; i++) {
  console.log(i);
}
52 chars
4 lines

In the example above, i starts at 1, and the loop continues as long as i is less than or equal to 10. The loop increments i by 1 on each iteration.

You can also use the step parameter to loop over a range with a different step value. For example, to loop over the even numbers from 2 to 10:

index.tsx
for (let i = 2; i <= 10; i += 2) {
  console.log(i);
}
55 chars
4 lines

In this case, i starts at 2, the loop continues as long as i is less than or equal to 10 and i is incremented by 2 on each iteration.

related categories

gistlibby LogSnag