Javascript in Hindi Loop




Javascript मे Loop बहुत उपयोगी होता है. Loop का प्रयोग हम Code को उसी Line मे बार बार Execute करने के लिए करते है और तब तक Code को Execute करते हैं जब तक कोई एक Condition True हो.

मान ले कि यदि आप अपने Web Page मे Hii MSG को 10 बार Type करना चाहते है तो आप उसको उसी Line को 10 बार Type कर सकते है. लेकिन अगर आप Loop का उपयोग करते है तो आप केवल 2, 3 Line मे पूर्ण हो सकते हैं .

Type of Loop

Loop Four प्रकार के होते है.

  • The For Loop

  • The While Loop

  • The Do while Loop

  • The For in Loop

The For Loop

JavaScript Program मे कोई ऐसा Statement है या Block of Statements है जिसे आप Multiple Times Execute कराना चाहते है तो इस Situation मे आप For Loop का उपयोग कर सकते है. For Loop मे तीन Expressions उपयोग किये जाते है Initialization Expression, Condition Expression और Update Expression.

Syntax

<script>
   for(initialize ; test ; increment/decrement)
   {
      statement(s);
   }
</script>

For Example

<!DOCTYPE html>
<html>
   <head>
     <title>JavaScript For Loop Example</title>
   </head>
   <body>
      <script type="text/javascript"> 
         for (i=1; i<=10; i++)  
         {  
            document.write(i + "<br/>")  
         }  
      </script>  
   </body>
</html>   								

Output

The While Loop

While Loop मे सबसे पहले Condition Check की जाती है अगर Condition True हो तभी While Loop के Inside Statements को Execute किया जाता है. While Loop के Inside Statements को Execute करने के बाद दोबारा से Condition को Check की जाती है अगर इस बार भी Condition True होती है तो फिर से While Loop के Inside Statements को Execute किया जाता है और यह Process तब तक चलता रहता है जब तक की Condition False ना हो जाये. Condition के False होते ही Loop को Terminate कर दिया जाता है.

अगर Condition पहली बार में ही False हो जाये तब While Loop के Inside Statements को बिना Execute किये ही While Loop को Terminate कर दिया जाता है.

Syntax

<script>
   while (condition)  
   {  
     code to be executed  
   }  
</script>

For Example

<!DOCTYPE html>
<html>
   <head>
      <title>JavaScript While Loop Example</title>
   </head>
   <body>
      <script type="text/javascript"> 
         var i=10;  
         while (i<=20)  
            {  
               document.write(i + "<br/>");  
               i++;  
            }  
      </script>  
   </body>
</html>   								

Output

The Do while Loop

JavaScript Program मे कई ऐसे Statement या Block of Statements होते है जिसे आप Multiple Times Execute कराना चाहते है तो इस Situation मे आप do..while Loop का उपयोग कर सकते है.

Do while Loop एक Block of Code को बार बार तब तक Execute करता रहता है जब तक की Do while Loop मे दी गई Condition True रहती है Condition के False होते ही Loop को Terminate कर दिया जाता है.

Syntax

<script>
   do
   {
      block of code to be executed
   }  
</script>

For Example

<!DOCTYPE html>
<html>
   <head>
      <title>JavaScript Do While Loop Example</title>
   </head>
   <body>
      <script type="text/javascript">
         document.write("<b>Using do...while loops </b><br />");
         var i = 2;
         document.write("Even numbers less than 20<br />");
         do
         {
            document.write(i + "<br />");

            i = i + 2;

         }
         while(i<20)
      </script>  
   </body>
</html>   								

Output

The For in Loop

For in Loop का उपयोग Array और Object के साथ काम करने के लिये किया जाता है.

Syntax

<script>
   for(index in arrayName)
   {
      // Code Block
   }
</script>

For Example

<!DOCTYPE html>
<html>
   <head>
      <title>JavaScript Do For in Loop Example</title>
   </head>
   <body>
      <script type="text/javascript">
         function demo(){
            var phone = new Array(5);
            phone[0] = "Apple";
            phone[1] = "Samsung";
            phone[2] = "Sony";
            phone[3] = "Xolo";
            phone[4] = "Nokia";      
            phone[5] = "Apple6";      
            phone[6] = "Apple7";      
            phone[7] = "Apple8";      
            for (var i in phone)
            {
               document.write("phone[" + i + "]  = " + phone[i] + "<br/>");        
            } 

         }
      </script>  
      <p>Click the button below</p>
      <button onclick="demo()">loop: For in</button>
   </body>
</html>   								

Output