Learn JavaScript WHILE LOOPS in 8 minutes! (8:12)
While loops repeat code while a condition is true. Learn how to create loops that continue indefinitely until a specific condition is met, validate user input, and avoid common pitfalls like infinite loops.
- While loop syntax and structure
- Creating loops that repeat until a condition changes
- Input validation with while loops
- Avoiding infinite loops
- When to use while vs for loops
- Break and continue statements
while (condition) {
// Code to repeat while condition is true
}How it works:
- Check if condition is true
- If true, execute code block
- Repeat from step 1
- If false, skip loop and continue
let username = "";
if (username === "") {
console.log("You didn't enter your name");
} else {
console.log(`Hello ${username}`);
}
// Runs once, no retry if emptylet username = "";
while (username === "") {
username = window.prompt("Enter your name:");
}
console.log(`Hello ${username}`);
// Keeps asking until user enters somethinglet userName = "";
while (userName === "" || userName === null) {
userName = window.prompt("Enter your name:");
}
console.log(`Hello ${userName}!`);How it works:
- Loop continues while username is empty OR null
- Keeps prompting until valid input
- Prevents empty submissions
let userName = "";
do {
userName = window.prompt("Enter your name:");
} while (userName === "" || userName === null);
console.log(`Hello ${userName}!`);Difference:
- While loop: Checks condition first, may not run at all
- Do-while loop: Runs at least once, then checks condition
- Both achieve same result for input validation
let loggedIn = false;
let username;
let password;
while (!loggedIn) {
username = window.prompt("Enter your username:");
password = window.prompt("Enter your password:");
// Check for empty or cancelled input
if (username === null || username === "") {
console.log("Username cannot be empty.");
continue; // Skip to next iteration
}
if (password === null || password === "") {
console.log("Password cannot be empty.");
continue; // Skip to next iteration
}
// Validate credentials
if (username === "myUsername" && password === "myPassword") {
loggedIn = true;
console.log("Login successful!");
} else {
console.log("Incorrect username or password. Try again.");
}
}
console.log("You are now logged in!");How it works:
- Loop runs while
!loggedInis true - Continue statement skips rest of iteration and starts next one
- Validates for empty inputs first
- Then checks credentials
- Only sets
loggedIn = trueon successful match - Provides clear feedback for each error type
let count = 10;
while (count > 0) {
console.log(count);
count--;
}
console.log("Happy New Year!");
// Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Happy New Year!The continue statement is crucial in the login example above:
if (username === null || username === "") {
console.log("Username cannot be empty.");
continue; // Jumps back to while condition check
}
// Code here is skipped when continue executesWhat continue does:
- Immediately stops current iteration
- Jumps back to the while condition check
- If condition is still true, starts next iteration
- Skips any code below it in the loop
Without continue:
// Would need nested if statements
if (username !== null && username !== "") {
if (password !== null && password !== "") {
// Validate credentials
}
}With continue:
// Cleaner, easier to read
if (username === null || username === "") {
console.log("Error");
continue;
}
if (password === null || password === "") {
console.log("Error");
continue;
}
// Validate credentialsconst correctPassword = "password123";
let userPassword = "";
let attempts = 0;
const maxAttempts = 3;
while (userPassword !== correctPassword && attempts < maxAttempts) {
userPassword = window.prompt("Enter password:");
attempts++;
if (userPassword !== correctPassword) {
console.log(`Incorrect. ${maxAttempts - attempts} attempts remaining.`);
}
}
if (userPassword === correctPassword) {
console.log("Login successful!");
} else {
console.log("Account locked!");
}// ❌ INFINITE LOOP - WILL CRASH!
let username = "";
while (username === "") {
console.log("You didn't enter your name");
// username never changes - loops forever!
}// ✅ Condition can become false
let username = "";
while (username === "") {
username = window.prompt("Enter your name:");
// username can change, allowing loop to exit
}Exits the loop immediately
let count = 1;
while (count <= 10) {
if (count === 5) {
break; // Exit loop when count is 5
}
console.log(count);
count++;
}
// Output: 1, 2, 3, 4Skips current iteration, continues loop
let count = 0;
while (count < 10) {
count++;
if (count === 5) {
continue; // Skip 5, continue with 6
}
console.log(count);
}
// Output: 1, 2, 3, 4, 6, 7, 8, 9, 10 (skips 5)Checks condition first
let num = 10;
while (num < 10) {
console.log(num); // Never runs
}Runs at least once, then checks condition
let num = 10;
do {
console.log(num); // Runs once: prints 10
} while (num < 10);// Keep asking until user enters an email with '@'
let email = "";
// Your code here// Keep asking until user enters a positive number
let number = -1;
// Your code here// Display menu until user selects 'quit'
let choice = "";
// Your code here// Keep accepting numbers until user enters 0, then show sum
let sum = 0;
// Your code herelet input;
while (!isValid(input)) {
input = getInput();
}let count = 0;
while (count < max) {
// Do something
count++;
}let found = false;
let index = 0;
while (!found && index < array.length) {
if (array[index] === target) {
found = true;
}
index++;
}let success = false;
let attempts = 0;
while (!success && attempts < maxAttempts) {
success = tryOperation();
attempts++;
}let isPlaying = true;
let health = 100;
while (isPlaying && health > 0) {
let action = window.prompt("What do you do? (attack/defend/quit)");
if (action === "attack") {
console.log("You attack the enemy!");
health -= 10; // Take damage
} else if (action === "defend") {
console.log("You defend!");
health += 5; // Heal a bit
} else if (action === "quit") {
isPlaying = false;
}
console.log(`Health: ${health}`);
}
console.log("Game Over!");// ❌ Infinite loop
let count = 0;
while (count < 10) {
console.log(count);
// Forgot to increment count!
}
// ✅ Correct
let count = 0;
while (count < 10) {
console.log(count);
count++; // Updates condition
}// ❌ Uses assignment instead of comparison
while (x = 10) { } // Always true!
// ✅ Correct
while (x === 10) { }// ❌ Runs 11 times (0 through 10)
let i = 0;
while (i <= 10) {
console.log(i);
i++;
}
// ✅ Runs 10 times (0 through 9)
let i = 0;
while (i < 10) {
console.log(i);
i++;
}- While loops repeat code while condition is true
- Check condition before each iteration
- Must have a way for condition to become false
- Perfect for input validation
- Infinite loops occur when condition never changes
- Use break to exit loop early
- Use continue to skip current iteration
- Do-while runs at least once
- While loops great for unknown iterations
- For loops better for known iterations
Duration: 8:12
Difficulty: Beginner
Category: Loops & Control Flow