Skip to content

#17 πŸ›‘ Loops in JS | For loop in javascript |

Adarsh Tripathi edited this page Jul 29, 2021 · 2 revisions

*****************************Tutorial Start πŸ”₯ ********************************

Loops in JS | For-loop in js

What is Loops?

Loops are used to execute the same block of code again and again, so long as a certain condition is met.

The basic idea behind the loop is to automate the repititive tasks within a program to save the time and effort.

Javascript now support five different types of loops:

  • for..loop
  • while loop
  • do..while loop
  • for..in loop (ye loop object ke property ko traverse karna ke liye use hota hai. ye related hai object se)
  • for..of loop (ye basically travel object ko retrieve karta hai. Repeat karne wale elements ko ye retrieve kar deta hai, example: strings(ye strings ko traverse kar dega, aik aik karke characters nikaal dega.), array(array ko elements ko retreive karke nikaal dega) jo retreivable hoge usko ye retrieve kar dega. jisme hum aik aik elements, characters nikaal sakte hai.)

what is For Loop?

Repeat the statement as long as certain conditions met.

jaisi voo condition true hogi, loop band hojayega.

<script>
         //structure of for loop
         for(initialization; condition; increment) {
            /* initialization--> ye voo variable hai jo track karega loop ko. Basically hamara loop hai kaha.
                initialization--> ye basically variable hoga.
                initialization--> isko hum skip bhi kar sakte hai.
            */
           /* consition--> condition matlab true yaa false, jaise condition false hoga ye loop band hojayega.
            And condition true hai too body execute ho jayegi. */

           /* incrememet, decrement ka matlab steps hota hai, ki hum kitna steps follow kar rhe hai , 1 , 1 karke ke aagye 
           jaa rhe hai, 2 2 kar ke aagye jaa rhe hai , ki 1 , 1 karke ke piche aa rhe hai , yaa 12 12 karke ke aagye bar rhe hai.
           */
     
             //code executed here

         }
     </script>
        initialization ---> start
        condition ---> end      (humko yaha aisaa set karna hoga ki condition meri false ho jaaye)
        increment ---> steps

Flow of For loop

    1. First-> initialization
    2. Second-> condition
    3. Third-> body execution (code to be executed here) 
    4. Fourth-> increment    

Initialization sirf first time pe hoga., dusri baar se (condition, execution, increment/decrement)

***************************** Tutorial End πŸš€ ********************************

Clone this wiki locally