Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

While & Do While Loops in JavaScript

While & Do While statements keep executing a block of statements in a loop until a given condition evaluates to false.

While loop

The Syntax of the While loop is as shown below

                            
while (condition) {
 
  // (While body)
  // statements to execute as long as the condition is true 
  
}
  
                        
                            
                        

While loop executes its body only if the condition is true.

How it works

  1. The loop evaluates its condition.
  2. If the condition is true then the While body executes. If the condition is false loop ends.
  3. Goto to step 2
Useful References

While Loop Example

                            
let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}
 
** Console ***
 

 
 
                            
                        
                            
                        

Do While Loop

The syntax of do While loop is as shown below.

In the following example, we remove all the break statements. Here the execution starts from the - operator but does not end in that case clause. The execution continues to * ,/ & even to the default clause.

                            

do {
 
 // (While body)
 // statements to execute as long as the condition is true 


} while (condition);
 
                            
                        
                            
                        

The do while loop tests its condition after it executes the while body. Hence the while body executes at least once.

How it works

  1. The loop body executes
  2. loop evaluates its condition.
  3. If the condition is true the loop body executes. If the condition is false loop ends
  4. Goto to step 2
Useful References

Do While Loop Example

                            
let k = 100;
do {
  console.log(k);
  k++;
} while (k < 105);
 
*** Console ***
100
101
102
103
104
 
 
                            
                        
                            
                        

Use Break to break out of the loop

We can use the break statement to break out of a loop

                            
 let j = 200;
 
 do {
  
   console.log(j);
   j++;
  
   if (j > 205) break;
  
  
 } while (true);
  
 **Console **
 200
 201
 202
 203
 204
 205