How to use for Loop In Javascript

In this method, a “for” loop is used to iterate through a given range of numbers. It identifies even numbers within this range by checking if the current number’s remainder, when divided by 2, is zero. If so, it prints the even number.

Example: To print all the even number within a specific range in JavaScript using for loop.

Javascript




// JavaScript program to print all even
// numbers in a range using for loop
let start = 4;
let end = 15;
 
for (let even = start; even <= end; even += 2) {
  console.log(even);
}


Output

4
6
8
10
12
14

Time Complexity: O(n)

Space Complexity: O(1)

Print all Even Numbers in a Range in JavaScript Array

We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer.

There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows:

Table of Content

  • Using for Loop in JavaScript
  • Using While Loop in JavaScript
  • Using forEach Loop in JavaScript

Similar Reads

Using for Loop in JavaScript

In this method, a “for” loop is used to iterate through a given range of numbers. It identifies even numbers within this range by checking if the current number’s remainder, when divided by 2, is zero. If so, it prints the even number....

Using While Loop in JavaScript

...

Using forEach Loop in JavaScript

In this method, we uses a “while” loop to iteratively identify and print even numbers within a specified range. The loop continues until the end of the range is reached....