While Loops

While loops are used when you need to repeat code, but you don't know exactly how many times the loop should run. They continue executing as long as a condition remains true.

Basic While Loop Syntax

A while loop starts with the while keyword, followed by a condition in parentheses (), and a block of code in curly braces {}:

let i = 0
while (i < 5) {
  // Code to repeat
  console.log(`Count: ${i}`)
  i++ // Important: Don't forget to change i!
}

The loop continues as long as the condition in parentheses is true and stops when it becomes false.

Converting For Loop to While Loop

Here's how a for loop translates to a while loop:

// For loop
for (let i = 0; i < 5; i++) {
  console.log(i)
}

// Equivalent while loop
let i = 0 // Initialization (outside the loop)
while (i < 5) { // Condition (same as for loop)
  console.log(i) // Code to run
  i++ // Increment (inside the loop)
}

Both print: 0, 1, 2, 3, 4

When to Use While Loops

  1. Unknown Number of Iterations

    // Keep asking for input until user enters "quit"
    let userInput = ""
    while (userInput !== "quit") {
      userInput = prompt("Enter a command (or 'quit' to exit):")
      if (userInput !== "quit") {
        console.log(`You entered: ${userInput}`)
      }
    }
    
  2. Processing Nested Data Structures

    const person = {
      name: "Kyle",
      friend: {
        name: "Joe",
        friend: {
          name: "Sally",
          friend: null, // Sally has no friend
        },
      },
    }
    
    // Get the final friend in the chain
    let currentPerson = person
    while (currentPerson.friend !== null) {
      currentPerson = currentPerson.friend
      console.log(`Current friend: ${currentPerson.name}`)
    }
    
    console.log(`Final friend: ${currentPerson.name}`) // "Final friend: Sally"
    

    You can't easily do this with a for loop because you don't know how deep the friend chain goes!

While Loop vs For Loop

Use a for loop when:

  • You know exactly how many times to loop
  • You're counting or iterating through arrays
  • You have a clear start, end, and increment

Use a while loop when:

  • You don't know how many iterations you need
  • You need to access nested or linked data

Loop Control: break and continue

Just like with for loops, you can use break and continue in while loops.

Do-While Loops

A variation that runs the code block at least once before checking the condition:

let userInput
do {
  userInput = prompt("Enter 'yes' to continue:")
  console.log(`You entered: ${userInput}`)
} while (userInput !== "yes")

The key difference: do-while checks the condition after running the code block.