DONATE  
MENU
MYSQL
CSS
HTML
JAVASCRIPT
AJAX
PHP
JSON

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Loops » do...while Loop
Share Tweet WhatsApp

do...while Loop

The do...while loop is almost same thing as the while loop. At least, they have a "while" in them but unlike the while loop, its conditions for continuous execution comes last. This means it executes its code block before checking if a condition is true or not. The while loop checks if a condition is true before executing any statements(s) but the do...while loop executes before checking if a condition is true or false. If it is true, it continues execution else it stops execution.
<script>
var arr = ["I", "am", "a", "Stack", "Beginner"];
var i = 0;
do {
alert(arr[i]);
i++; //increment to avoid infinite looping
}
while (i < arr.length)
</script>

Copy PRACTICE IT

If you don't understand the do...while loop yet, don't panic as you will get to understand it. See it from this angle where you use the do...while loop to tell JavaScript to do something and do it again if the condition for doing that thing is still true. First, it does the thing and then, check if the condition is true or false before knowing what to do next. Difference B/W while & do...while Loops Let's use some examples to try understand the difference between the while loop and the do...while loop.
<script>
var i = 0;
while (i != 0){
alert(true);
}
</script>

Copy PRACTICE IT

In the above, a variable "i" was initially with zero(0). A while loop was then created to execute a statement as long as variable "i" is not equal to zero(0). The while loop then checks if the variable is equal to zero or not before doing anything. Since it is equal to zero, it therefore does nothing. do...while loop equivalent
<script>
var i = 0;
do {
alert(true);
}
while (i != 0)
</script>

Copy PRACTICE IT

Above is the do...while loop equivalent of the initial codes. As you can see, it executes. This is because it executes first before checking if a given condition is true or false. SUMMARY
  1. The do...while loop is almost like the while loop.
  2. But unlike the while loop, it does something before checking to see if the condition given to it is true or not.
  3. if the condition is true, it repeats its execution else it ends.
  4. Therefore, use the while loop when you want it to check a condition before executing and the do...while loop when you want just an execution before checking a condition.
Take a short quiz on do...while Loop.
Ask a question or tell us if do...while Loop is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode