Mastering JavaScript: In-Depth While Loop Examples

Sarah Jul 13, 2026

While loops in JavaScript are a powerful tool for executing a block of code repeatedly based on a given condition. They're essential for automating repetitive tasks, summing arrays, and more. Let's dive into a comprehensive guide on JavaScript while loops, accompanied by practical examples.

πŸš€ Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! πŸ’»βœ¨
πŸš€ Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! πŸ’»βœ¨

The basic syntax of a while loop in JavaScript is as follows:

Master JavaScript Loops: For, While & Do-While Explained
Master JavaScript Loops: For, While & Do-While Explained

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

The loop continues to run as long as the given condition evaluates to true. Once the condition becomes false, the loop terminates and the code execution continues with the next statement following the loop.

Initializing and Incrementing a Counter

Explain For, While, Do-While Loops in JS and more
Explain For, While, Do-While Loops in JS and more

One of the most common uses of a while loop is to initialize and increment a counter. This is useful for iterating over elements in an array, or repeating an action a certain number of times.

Let's look at an example where we want to count down from 10 to 1, printing each number on a new line:

JavaScript while Loop: Repeat Tasks Efficiently πŸ”„
JavaScript while Loop: Repeat Tasks Efficiently πŸ”„

let i = 10;
while (i > 0) {
  console.log(i);
  i--;
}

In this example, the loop continues to run as long as i is greater than 0. After each iteration, we decrement i by 1. Once i becomes 0 or less, the loop terminates.

Infinite Loops and Breaking

Infinite loops can occur when the condition never becomes false. It's essential to be cautious when writing while loops to avoid infinite loops. To exit a while loop prematurely, you can use the break statement:

an image of a flow diagram with two different types of loops and loops on it
an image of a flow diagram with two different types of loops and loops on it

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

In this example, the loop runs indefinitely until i becomes greater than 5, at which point the break statement is triggered and the loop terminates.

Do While Loops

The do-while loop in JavaScript is similar to the while loop, but the condition is tested at the end of the loop instead of the beginning. This ensures that the loop body is executed at least once:

Cooking with Code: While and Do-While Loops in Apex & Java
Cooking with Code: While and Do-While Loops in Apex & Java

let i = 10;
do {
  console.log(i);
  i--;
} while (i > 0);

The loop continues to run until i becomes less than or equal to 0. This loop will always execute at least once, unlike a while loop where the first execution is not guaranteed if the initial condition is false.

Using While Loops with Arrays and Objects

Cheat Cheat on JavaScript Loops
Cheat Cheat on JavaScript Loops
JavaScript For Loop – Explained with Examples
JavaScript For Loop – Explained with Examples
a black background with white text that reads a full example
a black background with white text that reads a full example
Do While Loop in JavaScript | Example Program
Do While Loop in JavaScript | Example Program
Coding with Parvez
Coding with Parvez
Decoding the JavaScript Event Loop: Why Your Code Doesn’t Have to Wait in Line
Decoding the JavaScript Event Loop: Why Your Code Doesn’t Have to Wait in Line
Writing a while Loop in JavaScript
Writing a while Loop in JavaScript
the solution for loops is to create a program that allows you to use numbers and symbols
the solution for loops is to create a program that allows you to use numbers and symbols
Cooking with Code: While and Do-While Loops in Apex & Java
Cooking with Code: While and Do-While Loops in Apex & Java
JavaScript loops by Duomly
JavaScript loops by Duomly
JavaScript for Loop: The Ultimate Tool for Repeating Tasks πŸ”„
JavaScript for Loop: The Ultimate Tool for Repeating Tasks πŸ”„
a flow diagram with the words for loop, while loop and start are labeled in different colors
a flow diagram with the words for loop, while loop and start are labeled in different colors
Writing a do while loop in JavaScript
Writing a do while loop in JavaScript
Javascript Loops & Iterations
Javascript Loops & Iterations
an image of a computer screen with some text on it
an image of a computer screen with some text on it
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
an image of a computer screen with different colors
an image of a computer screen with different colors
Introducing 4 Different Types Of JavaScript Loops With Examples
Introducing 4 Different Types Of JavaScript Loops With Examples
For, While and Do While LOOP in JavaScript (with Example)
For, While and Do While LOOP in JavaScript (with Example)
a flow diagram showing the process to test and maintain loop sizes for an appliance
a flow diagram showing the process to test and maintain loop sizes for an appliance

While loops can also be used to iterate over the properties of an object or the elements of an array.

Here's an example of using a while loop to sum all the numbers in an array:

let numbers = [1, 2, 3, 4, 5];
let sum = 0;
let i = 0;
while (i < numbers.length) {
  sum += numbers[i];
  i++;
}
console.log(sum); // Outputs: 15

In this example, we initialize a counter i to 0, and the loop continues as long as i is less than the length of the numbers array. We increment the counter and add the current element to the sum variable in each iteration.

Iterating Over Object Properties

You can use a while loop to iterate over the properties of an object using the Object.keys() method:

let person = {
  name: "John",
  age: 30,
  city: "New York"
};
let keys = Object.keys(person);
let i = 0;
while (i < keys.length) {
  console.log(`${keys[i]}: ${person[keys[i]]}`);
  i++;
}

In this example, we first get an array of the object's keys using Object.keys(). We then use a while loop to access and log each property and its value.

While loops in JavaScript provide a flexible way to repeat a block of code based on a given condition. Mastering while loops will help you tackle a wide range of programming challenges, from summing arrays to automating repetitive tasks.