Skip to main content
D
JavaScript
JavaScript While Loop

JavaScript While Loop

JavaScript while loops repeat code as long as a condition remains true.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

JavaScript uses the `while` loop when developers don't know exactly how many times the loop should run. It simply checks a condition, runs the code, and checks the condition again.

Example

Example
1/* this loops until i is 10 */
2let i = 0;
3while (i < 10) {
4  console.log(i);
5  i++;
6}

Key Points

  • The loop runs continuously while the condition is true.
  • The loop stops instantly when the condition becomes false.
  • You must update the condition inside the loop.
  • Forgetting to update the condition causes an infinite loop.

Up Next

Continue your journey with the next topic.

Go to JS Break