What is JavaScript While Loop?

Video Tutorial
FREE
while and do while thumbnail
This video belongs to
JavaScript Course With Certification: Unlocking the Power of JavaScript
9 modules
Certificate
Topics Covered

In Javascript, a while loop is a control flow statement that allows code to be run repeatedly based on the given condition. Javascript while loop takes a condition, and statements of the while loop is executed as long as the condition is satisfied. While loop is also known as a pretest loop or entry-controlled loop as it evaluates the expression before each iteration.

Flow Chart

The flow chart for the while loop looks as follows: JS while loop flow chart

As shown in the flowchart, Statements are executed as long as the condition is true, if the condition is false, then the loop will end.

Syntax

While loop follows a very simple and easy syntax:

Examples

Now, we know about the while loop in JavaScript. Let's see some examples of how it works.

Example 1: Using While loop

In this example, we will apply the while loop simply till the counter value reaches the given value. This simple example will make the use of javascript while loop clear.

Output: In every iteration value of i gets incremented and printed on the console till the value of i reaches 10.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example 2: Using an Assignment as a Condition,

we can also assign variable values and can use it as a condition, the variable's value can be changed by using the statements of the javascript while loop.

Output: In every iteration value of a is assigned from variable i, and i gets incremented till the loop ends.

The Do...While Loop

In Javascript, do...while loop is similar to the while loop, except that the condition is checked at the end of the loop. It is also called an exit-controlled loop. So we can conclude that the loop will be executed at least once even if the condition does not satisfy (i.e., false).

Flow Chart

The flow chart for the do-while loop looks as follows: JavaScript do-while loop flow chart

As shown in the flowchart, the statements are executed first, then the conditions are checked if they satisfy or not.

Syntax

do...while loop follows a very simple and easy syntax:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Examples

Now, we know about the do...while loop in JavaScript, So now take a look at some examples of how it works.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example 1: Using the do-While loop

In this example, we will use the do...while loop to print numbers from 1-20.

Output: In every iteration value of i gets incremented and printed on the console till the value of i reaches 20. After every iteration, the value is checked for the condition.

Example 2: Running do-while at least once even if the condition is false

In this example, we have set variable a to false, and conditions are checking for the value of a as true. But by the property of do-while, statements are executed before checking the conditions.

Output: Here, the loop got executed only once.

Comparison Between while and do-while Loop

Now let's take a look at some differences between the while and do-while loop:

whiledo-while
while loop checks the condition first and after that executes the statementsdo-while loop will execute the statements at least once after that, the condition is checked.
while loop is also called entry controlled loopdo-while is also called exit controlled loop
while loop statements are not executed if the condition is falsedo-while loop statements are executed at least once if the condition is false

Browser Compatibility

Compatible Browsers are:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Edge

Conclusion

  • while loop is a control flow statement that allows code to be run repeatedly based on the given condition.
  • while loop is also known as a pretest loop or entry controlled loop.
  • do...while loop is similar to the while loop except that the condition is checked at the end of the loop.
  • do...while loop is also called an exit-controlled loop.
  • do...while loop will be executed at least once even if the condition does not satisfy (i.e., false).

See More

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more