Loops in javascript

Introduction

Javascript is a programming language and one of the most important parts of a webpage with HTML and CSS. Javascript is a high-level language and is widely popular among developers and most webpages are using javascript. earlier webpages were static to make webpages more interactive and dynamic the javascript language is developed to cater to these problems so javascript is an interactive and dynamic programming language. it is widely used as a client-side programming language and the javascript interpreter is available in all web browsers. There are a lot of similar naming languages there so the clear confusion the javascript is built on the ECMAScript standards. javascript is still a relevant language today its popularity increasing day by day after the introduction of node.js most companies use it as a backend language or server-side language too. javascript helps in creating more interactive web pages by creating a pop-up message, alert pop-up, links, and validating text in the text box. There are lots of topics in javascript such as array, loops, objects, functions, object methods, strings, dom, and many more topics. In this article, we are going to discuss loops in JavaScript. what are loops and how many types of loops are there the importance of loop and their syntax how to write a simple loop program in javascript?


Understanding Loops In Javascript

Loops basically mean something which happens again and again just like you laugh sometimes in loops and sometimes you eat your favorite dish again and again. In JavaScript, the loop can be defined as a repetition of some process again and again to a certain process limit till the condition is not satisfied by using a single command or single line of code.

If you want to print 1 number 10 times so to print a 1 the code for this would be {console.log(1) } and this code prints 1 only one time but the condition is to print ten times so in that case we have to write the same line of code ten times to solve this problem we used loops in javascript and many more programming languages. you can try it

try it using your browser 

Step 1: right-click on the webpage and click on the inspect option then go to the console and write your code.

ex: console.log(1);
      console.log("Your name");
and one of world's most popular phrases or codes is print hello world
    console.log("Hello world");


so loops are basically used when we want to perform some task multiple times or until the condition is not satisfied. there is the proper syntax to apply loops in different languages and here we are using javascript syntax as we are learning loops using javascript before that we have to know the types of loops in javascript. 

loops in java script , for loop, while loop, do while loop, nested loop
for loop




👉Types of loops in javascript 


      1. While loop
             1.1 Nested while loop
      2. Do  while loop 
      3. for loop
           3.1 Nested for loop 
      4. For in loop 

Before going to a particular type of loop we should know how to access each item in a given set of problems so here we use a process called iteration to access each item.

There are three very important things in a loop that is iteration, condition, and increment/decrement.

👉 While loop 

while loop is used for iterating numbers for infinite times and the variable is assigned separately while coding with proper conditions let's check below how to write properly.

The syntax  for the while loop :
   let i=0   // initialization or iteration
    while (condition){
             code here
             increment  // i++ 
            };

   
 Example 1;
 Suppose we have to print soccer six times using a while loop.


let i=1; // first step
 Here we are using the variable let i=1 to initialize declare or iterate the starting point that we have to start from1.

while (i<=6) // second step

Here we are using the while with the condition inside the bracket. The condition is that we have to print the soccer till the value of i is less than six or equal to six. Once the condition becomes false it will stop the process.

{
 console.log("soccer"); // Third step ;

In the third step, we are using console.log to print the soccer 

i++; // fourth step  Here we are incrementing the value of i. if it is i=1; after incrementing it becomes              i=2;


clear code ;

let i=1; 
 while (i<=6){
console.log("soccer");
i++;
}
Note: if you forget to write an increment condition then the loops go till infinity and your will hand due to this most this the common mistake is made by beginners or even developers also.


There are mainly four main things initialization, condition, code, and increment/decrement 

Increment means to increase the value of an existing one.  Let we have   i=1  now if we want to increment  it by a value of 1 each time until and unless it reaches the  given limit suppose the given limit is 6 

so the first i=1; 
             increment it (i++, syntax used in javascript)
 now it becomes i=2; 
   again increment by 1;
now it becomes i=3; 
  again increment 
now, i=4;
now again increment 
i=5;
now again increment 
i=6; once it reaches the condition it will stop incrementing 

the same thing happens with decrement it decreases the value and the syntax for javascript is i--.


👉Do while loop

  
 Do while loop is a different form of a while loop. In the while loop, it first checks whether the condition is true or false but in the Do while loop it checks the condition later first it executes the loop, and it runs at least once.

      let i=54;
          do{
          console.log(i);
            i++;
          } while(i<=60)
    Nested while loop
         A nested while loop is a while loop inside a while loop so the loop inside is called a nested loop.


👉For loop 

 for loop is mostly the same as the while loop but all the conditions and the variable declaration and increment are written in the same line or together.

N=23;

for ( let i=0; i<=N; i++ ){

            console.log( i );

       };

👉Nested for loop 

     The loop contains a loop inside it called a Nested for loop.

    for( let i=0; i<=N; i++){

          for(let j=0;  j<=N; j++){
               console.log(j);

         };


For in loop 

This type of loop is used in a JavaScript object. for in loop iterates the properties of javascript objects. the object is quite different from arrays and strings. it has a key-value pair system and to access the particular value and key we used a special in loop to access the key-value pair in javascript.                                                            


Conclusion

Loops are a fundamental concept in JavaScript, enabling developers to perform repetitive tasks efficiently. Understanding different types of loops and their usage helps in writing cleaner and more efficient code. Whether it's iterating over arrays, and objects, or performing repeated actions, loops are an essential tool in a JavaScript developer's toolkit. For more in-depth tutorials and guides on JavaScript and other programming concepts, visit our comprehensive resources to enhance your coding skills.

Post a Comment

Previous Post Next Post