- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
do-while loop in JavaScript with examples
Are you tired of writing code that must be executed at least once, regardless of whether the condition is true or false? Then look no further than JavaScript's do-while loop! The do-while loop is a must-know tool for any JavaScript developer looking to improve their coding skills, thanks to its unique structure and powerful functionality.
In this post, we'll go over do-while loops in JavaScript, including how they differ from other types of loops and when to use them. So let's start with a brief definition.
The do-while loop in JavaScript is used to execute some block of code once, regardless of the condition given to the loop, and then continue executing the same block of code until that condition evaluates to be false.
JavaScript do-while loop syntax
The syntax of the do-while loop in JavaScript is:
do {
// body of the loop
} while(condition);
JavaScript's do-while loop is a particular kind of loop that runs a block of code at least once before repeating the execution of the block as long as a particular condition is still true.
Therefore, whatever block of code is written as the "body of the loop" will be executed once and then continue its execution until the specified "condition" evaluates to true. That is, first the "body of the loop" will be executed, then its "condition" will be evaluated; if it is true, then the "body of the loop" will again be executed, and then the "condition" again will be evaluated; if it is true, then program flow again enters the loop's body. This process continues until the "condition" evaluates to false.
JavaScript do-while loop example
Consider the following code as an example demonstrating the do-while loop in JavaScript:
<!DOCTYPE html> <html> <body> <script> let num = 6, i = 1; do { console.log(num*i); i++; } while(i<=10); </script> </body> </html>
The snapshot given below shows the sample output produced by the above JavaScript example:
Since the condition in the do-while loop is always given after the body of the loop, Therefore, the body of the loop will always be executed at once, regardless of the given condition. For example:
<!DOCTYPE html> <html> <body> <script> var x = 10; do { console.log("The value is: ", x); x++; } while(x>20); </script> </body> </html>
The output produced by the above example is:
In the above example, the condition x>20 or 11>20 (the value of x will be 11 after incrementing the value of x using the statement x++; available in the body of the loop) evaluates to false at first. Then also, the body of the loop was executed all at once or for the first time.
How is the do-while loop different from other types of loops?
JavaScript's do-while loop is a type of loop that resembles other loops like the "for" loop and "while" loop but differs in a few key ways.
- One significant distinction is that, whether the loop condition is true or false, the do-while loop always runs the code block at least once. This is due to the fact that the code block is executed once before the condition is checked because the loop condition is evaluated following the initial iteration of the loop. This is in contrast to the while loop, which checks the condition before executing the code block.
- Another distinction is that the do-while loop is typically used when the required number of iterations is unknown in advance or when you want to execute a block of code at least once. In contrast, the for loop is typically employed when the required number of iterations is known in advance and iteration over a range of values is required. The while loop is typically employed when iteration should continue as long as a certain condition is met.
Due to the fact that it always runs the code block at least once, even if the condition is immediately false, the do-while loop can be less effective than other loops. Performance overhead and pointless iterations may follow from this. As a result, it's crucial to carefully consider the use case and select the ideal loop construct for your particular situation.
« Previous Tutorial Next Tutorial »