The while loop in JavaScript

While writing a program, you may encounter a situation where you need to perform an iterative action. In these situations, you will need to write loop commands to reduce the number of lines of code.

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:

The while loop in JavaScript Picture 1

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:

The while loop in JavaScript Picture 2

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

4 ★ | 2 Vote

May be interested

  • For ... loop in JavaScriptPhoto of For ... loop in JavaScript
    the for ... in loop is used to iterate over the properties of an object. when we haven't discussed the object yet, you may not feel comfortable with this loop. but once you understand how objects work in javascript, you will find this loop very useful.
  • Loop control in JavaScriptPhoto of Loop control in JavaScript
    javascript provides high control to handle loop commands and switch commands. there may be a situation when you need to exit the loop without having to go to its endpoint. there may also be situations when you want to jump over a part of the code block and start the next loop.
  • JavaScript functionsPhoto of JavaScript functions
    a function is a group of reusable code that can be called anywhere in your program. this makes it unnecessary to write the same code over and over again. it helps programmers write modular code. functions allow a programmer to divide a large program into small and manageable functions.
  • Event (Event) in JavaScriptPhoto of Event (Event) in JavaScript
    javascript interaction with hmtl is handled through events that occur when the user or browser manipulates a page.
  • Page navigation (Redirect) in JavaScriptPhoto of Page navigation (Redirect) in JavaScript
    you may have a situation when you click on a url to go to page x but you are directed to page y. it happens because page redirection - redirects the page. this concept is different from: javascript - refresh page.
  • Dialogs - Alert, Prompt, Confirmation in JavaScriptPhoto of Dialogs - Alert, Prompt, Confirmation in JavaScript
    javascript supports 3 important dialog types. these dialogs can be used to notify, confirm input, or receive input from users. below we discuss each type of dialog box.