JavaScript Loops || Simple introduction to javaScript loops with types

JavaScript is used to repeat the number of code lines again and again. JavaScript loops are also called javascript repeaters or javascript iteration.

JavaScript Loops || Simple introduction to javaScript loops with types

JavaScript Loops in Simple Words

With the help of JavaScript loops, we can repeat any code as many times as we want without writing it. The concept of JavaScript loops is very simple but very important in the world of programming.


A good javascript developer reduces the number of lines in their project by using javascript loops, which makes the project code more unique.


Introduction to JavaScript DOM.


If you are a developer or become a developer and you don't have a concept of javascript loops then don't worry, you approach the correct article to understand javascript loops or javascript repeaters.


In today's article, you will learn about JavaScript loops with their types in very simple wording. 


This article will give you the concept of javascript loops in such a simple way that all your javascript loops confusion will be cleared. After reading this article till the end you will be an expert in the javascript loop.


Why do we learn JavaScript Loops?

Before learning JavaScript I would like to describe to you why we use JavaScript loops. Because if you don't know why we used javascript loops then you never understand the concept of javascript loops.


JavaScrppt loops are used to repeat any code over and over again. To repeat lines of code, we have to write this code again and again, but with the help of javascript loops, we will write any code just once and repeat it again and again. This is a very simple way to repeat and code again and again.


JavaScript Loops With Types

There are further four types of javascript loops. Below is a list of javascript loops.

  1. JavaScript For Loop
  2. JavaScript While Loop
  3. JavScript Do While Loop
  4. JavaScript forEach Loop

1. JavaScript For Loop

JavaScript for loop is an important and easy loop in JavaScript.  For loop is the most used loop as compared to another javascript loop because all the code conditions are written just in one line. 

Syntax of JavaScript For Loop


 for ( decalreVariable; variableLimit; variableIncrement/Decrement){

    document.write(+ X +"CodingPea<br>");

  }

 

Code example of JavaScript for loop

In the below code example, X is our var type variable the next is the variable limit and the next is a variable increment.


 for ( var X=1; X <=10; X++){

    document.write(+ X +"CodingPea<br>");

  }

 

2. JavaScript While Loop

While loop is the second top used and imports loop in javascript. The main difference is between the for loop and the while loop, in the for loop we declare the variable with the condition but in the while loop we declare the variable in above the while condition.

While Loop Syntax


 declare variable;

  while ( condition ){

    your statement;

    variable increment/decrement;

  } 

 

Code example of JavaScript While Loop


 var x= 1;

  while ( x <= 10 ){

    document.write(+ x +"CodingPea<br>");

    x = x+1;

  } 

 

3. JavaScript Do While Loop

Do while loop is the second main javascript loop. Do while loop is the same as while loop. The main difference between the while loop and the do-while loop, that in the while loop we write our statement under the condition but in the do-while loop we write the statement above the condition.

Syntax of Do While Loop


 declare variable

do{

Your statement//

}while(condition) 

 

4. JavaScript forEach Loop

ForEach loop is a bit different from the above three loops. We used for each loop just for array data types. forEach loop is also called the javascript inbuild function.

Syntax of ForEach loop in javascript


variableName forEach(function(){

your statement//////

});

Post a Comment

Previous Post Next Post