JavaScript for Loop In Hindi

📔 : Java Script 🔗

पिछले topic में हमें while loop के बारे में पढ़ा इस topic में हम For Loop के बार में पढ़ेंगे।

Actually किसी code of block को repeatedly run करने का सबसे easy way looping है , while loop या do while loop में अगर हम ध्यान से देखेंगे तो 3 steps को follow किया गया है -

  1. Initialization

  2. Condition

  3. And Increment / Decrement

while loop या do while loop में 3 steps हम अलग अलग लिखते थे।

अब For Loop में इन तीनो statements को अलग अलग लिखने की वजाय हम एक साथ लिखते है जिससे Looping और easy हो जाती है।

JavaScript में For Loop हम तब use करते हैं जब हमें पता हो कि Loop को कितने बार रन करना है , और यही main Difference है While Loop और For Loop में।

JavaScript for Lop Syntax

for(initialization ; condition ; increment / decrement)
{
  //code of block
}

तो Syntax में आप देख सकते हैं कि For Loop में , हम तीन Expression देते हैं , जो कुछ इस तरह से Run होते हैं।

  1. First Expression  For Loop में Initial Expression हैं जहाँ हम किसी variable को Define करते हैं ।

  2. Second Expression Conditional Expression होता है और हर iteration में second expression execute होता , Condition True होने पर ही  loop में Entry होती है otherwise हम Loop से बाहर हो जाते हैं। 

  3. सबसे last में Third Expression रन होता है , जहां पर हम किसी variable को  Increment / Decrement  करते हैं। यह भी हर iteration के last में ही execute होता है। , हालाँकि यह Optional होता है , यह variable हम Loop के अंदर भी Increment / Decrement कर सकते हैं ।

JavaScript for Loop Example

Print table of 5

let number = 5; for(let x=1; x<=10;x++) { document.write(number+' x '+x+' = '+(x*number)+'<br>'); }

Output :

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

इसके अलावा हम JavaScript में Array को For Loop की help से आसानी से iterate कर सकते हैं।

See Example :

let array_var =[12,234,45]; for(let index=0; index < array_var.length; index++) { document.write('Array Key : '+index+' , Value : '+ array_var[index] +'<br>'); }

Output :

Array Key : 0 , Value : 12
Array Key : 1 , Value : 234
Array Key : 2 , Value : 45

Example में length एक Predefined JavaScript keyword है जिसका use Iterate Object (String , Object , Array) की length जानने के लिए किया जाता है।

ठीक Nested If Else या Nested While Loop की तरह ही हम Nested For Loop (For Loop Inside Another For Loop) भी use कर सकते हैं।

See Another Example :

for(let x=1;x<=5; x++) { for(let y=1; y <= x; y++) { document.write(y+" "); } /*for line break*/ document.write("<br>"); }
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers