Today we are going to learn 5 type of JavaScript loops in detail with a complete tutorial and examples.
The simplest meaning of loop is to repeat something.
“A structure, series, or process, the end of which is connected to the beginning.”
In the programming term when we need to repeat a set of code statements for a specific time, we can use loops.
In the JavaScript we have the following loop concepts available.
You might be interested in jQuery clone a form elements with unique attributes.
JavaScript for loop
is a widely used looping statement. It is very easy to use as given in the example below.
Loop initiator, condition and increment/decrement statement all will be in the same line.
for (initiate Loop; Loop Condition; increment/decrement Statement) { // loop body }
var users = [ 'raj', 'neha', 'mukund', 'ravi', 'devid' ]; for( let i = 0; i < users.length; i++ ) { console.log('Hello - ' + users[i]); }
Console :
"Hello - raj"
"Hello - neha"
"Hello - mukund"
"Hello - ravi"
"Hello - devid"
JavaScript do while loop
is a looping statement which can be used if we want to run the loop at least once and then need to check the condition to allow for the next loop.
As its name suggests, do something while the condition is true. So, this loop executes its purpose at once and then test the condition in the while statement, if that is true then the loop will execute again otherwise control will pass to the next statement.
do { // loop body } while ( loop condition );
let j = 0; do { console.log('Hello - ' + users[j]); j++; } while ( j < users.length );
Console :
"Hello - raj"
"Hello - neha"
"Hello - mukund"
"Hello - ravi"
"Hello - devid"
JavaScript while loop
is similar to for loop, the only difference is loop initiator and increment/decrement statement.
We have to declare our initiator statement first and then the condition will pass to the while loop. Next, in the while loop body we need to define our increment/decrement statement.
//initiate loop; while ( loop condition ) { //loop body //increment/decrement statement }
let k = 0; while ( k < users.length ) { console.log('Hello - ' + users[k]); k++; }
Console :
"Hello - raj"
"Hello - neha"
"Hello - mukund"
"Hello - ravi"
"Hello - devid"
JavaScript for in
is another looping statement which is very interesting. Because here we don’t need loop initiator and increment decrement statement.
It will get the all indexes one by one from the array and terminate itself at the end of the array elements.
We just need a condition for this loop that is to execute the following statements until we have an element remaining in the array.
for ( index in array ) { // loop body }
for( index in users ) { console.log("Hello - " + users[index]); }
Console :
"Hello - raj"
"Hello - neha"
"Hello - mukund"
"Hello - ravi"
"Hello - devid"
JavaScript for of
is similar to for in
loop the major difference is that for in
loop retrieve the index and for of
loop retrieve the element value.
It will break the loop when all elements of the array or object will be iterated.
Again we just need a condition for this loop that is to execute the following statements until we have an element remaining in the array as in the JavaScript for in loop.
for ( element of array ) { // loop body }
for( user of users ) { console.log("Hello - " + user); }
Console :
"Hello - raj"
"Hello - neha"
"Hello - mukund"
"Hello - ravi"
"Hello - devid"
Open the console to see the output on codepen.
Hope you find this article useful.
See you in the next learning journey.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…