- Diwali Online Shopping Scams 2026: 10 Ways to Stay Safe
- 10 Best DDoS Protection Services for 2027
- 12 Best Web Application Firewalls (WAF) for 2027
- Best Ethical Hacking Tools in 2027: Free & Paid Picks
- Software supply chain attacks 2026: Timeline and developer checklist
- Binance Hack: $6.3 Billion Drained in the Largest Crypto Theft Ever. What We Know So Far
- This Site Uses XSLT Chrome Warning Explained: How to Fix It
- iPad mini OLED Price in India: Wi-Fi & Cellular Variants, Offers
A While Loop is a control flow statement that allows a part of the code to be executed repeatedly based on a given Boolean condition. The While Loop can be thought of as a repeating if statement.
Syntax of a While Loop
The syntax of a While Loop in JavaScript is as follows:
while (condition) {
// code to be executed
}
- Condition: This is a Boolean expression that is checked before each iteration of the loop. If it evaluates to
true, the loop continues; if it evaluates tofalse, the loop ends.
Example of a While Loop
Here’s a simple example of a While Loop that prints the numbers from 1 to 5:
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
In this example, i is initialized to 1, the condition checks if i is less than or equal to 5, and i is incremented by 1 at the end of each loop iteration. The console.log(i) inside the loop will print the numbers 1 through 5.
The While Loop is another powerful tool in JavaScript, allowing you to write more efficient and cleaner code. It’s a fundamental concept that’s widely used in programming, so understanding how it works will greatly enhance your JavaScript coding skills.



