How to Repeat a Task in JavaScript Until Desired Outcome

Updated on May 14, 2023

If you want to repeat the same task again and again until the desired outcome, you can use a JavaScript loop. Especially the while loop is well suited to get this job done.

It allows you to repeat a task until a specific condition is met. Inside the loop, you can perform any actions necessary to achieve your desired outcome, like manipulating data, running calculations, or executing other functions.

Today I will show you how to use a while loop in JavaScript to repeat a task until you reach your desired outcome.

Performing a Task Repeatedly Using JavaScript Loop

Suppose you want to generate a username automatically when a user creates a new account in your website. While signing up, users will enter their names, and based on their names your application will generate random usernames.

Usually, usernames are unique which means multiple users can't have the same username. That's why you also have to check whether a generated username already exists in your database or not.

          let generatedUsername
let hasUsername = false

while (!hasUsername) {
    // Generate username
    const firstName = 'max'

    const randomNumber = Math.floor(Math.random() * 9999 + 1)

    generatedUsername = `${firstName}${randomNumber}`

    // Check any user exists with "generatedUsername" in the database

    if (/* If there is no user */) {
        console.log('No user found')
        hasUsername = true
    } else {
        console.log('User already exists')

Here I have 2 variables: generatedUsername and hasUsername.

The generatedUsername will store the generated username, and hasUsername is a boolean value that keeps track of whether or not a unique username has been generated.

You will run the while loop if hasUsername is false that means the loop will run until we generate a unique username.

Inside the loop, generate a random number between 1 and 9999 using the Math.random() function. Then append it with the firstName to create a new username.

For example, if the random number generated is 2841, the username will be "max2841".

After getting a username, it's time to check whether any user is available or not with that username in your database. If there is no user available that means this username is unique.

In this case, you have to set hasUsername to true. This will end the while loop since this loop will continue running if hasUsername is false.

If any user is found in the database then this username is not unique. You will keep hasUsername variable as false. The loop will continue to generate new usernames by looping through the code again until a unique username is found.

Also Read: How to Call a Function N Number of Times in JavaScript


You have seen the technique to repeat a task using the JavaScript while loop. Here I used an example of generating a unique username by checking if it already exists in a database.

But you can utilize this technique to perform anything for your application. It will help you to continue doing something based on a condition.

It's important to remember that the while loop may create an infinite loop if you don't implement the condition correctly. Make sure that the loop condition eventually becomes false.

If the condition doesn't become false then the loop will continue running infinitely which will crash your application. Other than that, the while loop will help you to repeat a task in JavaScript until you get the desired outcome.

Related Posts