Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

The While Loop in JavaScript

Explore The While Loop in JavaScript with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers the while loop in JavaScript with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

JavaScript supports all the necessary loops to reduce the pressure of the program:

While Loop

The most basic loop in JavaScript is the while loop that will be discussed in this chapter. The purpose of the While Loop is to execute a command or repeated code block as long as the Expression - the Expression is True . Once the expression becomes False , the loop ends.

Implementation diagram

The diagram showing The while loop Implementation process is as follows:

Syntax

The following is The while loop Syntax in JavaScript:

 while ( expression ){ Statement ( s ) to be executed if expression is true } 

For example

You try the following example to execute a while loop.

 type = "text/javascript" > 
 Set the variable to different value and then try. 

Result:

 Starting Loop 
 Current Count: 0 
 Current Count: 1 
 Current Count: 2 
 Current Count: 3 
 Current Count: 4 
 Current Count: 5 
 Current Count: 6 
 Current Count: 7 
 Current Count: 8 
 Current Count: 9 
 Loop stopped! 
 ??t bi?n vào giá tr? khác nhau và th? th? . 

Do. While Loop

The Do. While Loop is similar to The while loop Except that the condition check occurs at the end of the loop. This means that the loop will always be executed at least once, even if the condition is False .

Implementation diagram

Here is the Do-while Loop implementation diagram:

Syntax

Do. While loop Syntax in JavaScript is as follows:

 do { Statement ( s ) to be executed ; } while ( expression ); 

Note - Don't forget the semicolon used at the end of the do. While loop.

For example

Try the following example to learn how to execute a Do. While loop In JavaScript.

 type = "text/javascript" > 
 Set the variable to different value and then try. 

Result:

 Starting Loop 
 Current Count: 0 
 Current Count: 1 
 Current Count: 2 
 Current Count: 3 
 Current Count: 4 
 Loop Stopped! 
 ??t bi?n vào giá tr? khác nhau và th? th? . 

Follow tutorialspoint

Previous article: Switch Case command in JavaScript

Next article: For loop in JavaScript

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.