JavaScript while Loop

In this chapter, we will learn about the JavaScript while loop. The while loop is used to execute a block of code as long as a specified condition is true. We will cover:

  • What is a while Loop?
  • Syntax
  • Using the while Loop
  • Using the do...while Loop
  • Breaking and Continuing in while Loops
  • Simple Programs using while Loops

What is a while Loop?

The while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The loop will continue to execute as long as the condition evaluates to true.

Syntax

while (condition) { // code to be executed } 
  • condition: The condition to be evaluated before each iteration. If true, the loop continues. If false, the loop ends.

Using the while Loop

Example

let i = 0; while (i < 5) { console.log(i); i++; } 

Output:

0 1 2 3 4 

In the example above, the loop starts with i = 0, prints the value of i, increments i by 1, and stops when i reaches 5.

Using the do…while Loop

The do...while loop is similar to the while loop, but it executes the block of code at least once before checking the condition.

Syntax

do { // code to be executed } while (condition); 

Example

let i = 0; do { console.log(i); i++; } while (i < 5); 

Output:

0 1 2 3 4 

In the example above, the loop prints the value of i and increments i by 1, then checks if i is less than 5. The loop stops when i reaches 5.

Breaking and Continuing in while Loops

The break statement is used to exit a loop early, while the continue statement is used to skip the current iteration and continue with the next iteration.

Example with break

let i = 0; while (i < 10) { if (i === 5) { break; } console.log(i); i++; } 

Output:

0 1 2 3 4 

Example with continue

let i = 0; while (i < 10) { i++; if (i === 5) { continue; } console.log(i); } 

Output:

1 2 3 4 6 7 8 9 10 

Simple Programs using while Loops

Program 1: Print Numbers from 1 to 10

let i = 1; while (i <= 10) { console.log(i); i++; } 

Output:

1 2 3 4 5 6 7 8 9 10 

Program 2: Calculate the Sum of the First 10 Natural Numbers

let sum = 0; let i = 1; while (i <= 10) { sum += i; i++; } console.log("Sum:", sum); 

Output:

Sum: 55 

Program 3: Find the Factorial of a Number

let number = 5; let factorial = 1; let i = 1; while (i <= number) { factorial *= i; i++; } console.log("Factorial:", factorial); 

Output:

Factorial: 120 

Program 4: Print the Elements of an Array

let fruits = ["apple", "banana", "mango"]; let i = 0; while (i < fruits.length) { console.log(fruits[i]); i++; } 

Output:

apple banana mango 

Program 5: Reverse a String

let str = "hello"; let reversedStr = ""; let i = str.length - 1; while (i >= 0) { reversedStr += str[i]; i--; } console.log("Reversed String:", reversedStr); 

Output:

Reversed String: olleh 

Conclusion

In this chapter, you learned about the JavaScript while loop, including its syntax, how to use it, the do...while loop, and breaking and continuing in while loops. We also covered various use cases with simple programs to demonstrate the usage of while loops. The while loop is used for repeating tasks based on a condition.

Leave a Comment

Scroll to Top