What is Repetition (Looping) in JavaScript? in Hindi
/ DIPLOMA_CSE / Web Technology
Repetition (Looping) in JavaScript
-
Repetition (Looping) in JavaScript
JavaScript में Repetition (Looping) का मतलब है एक ही काम को बार-बार करना। जब हमें कोई task कई बार करने की जरूरत होती है, तो हम loops का इस्तेमाल करते हैं। Loops हमें code को कम करने और repetitive tasks को आसानी से handle करने में मदद करते हैं। इस लेख में हम JavaScript के विभिन्न loops के बारे में जानेंगे, जो repetition के लिए उपयोगी होते हैं।
What is Repetition (Looping) in JavaScript?
Repetition (Looping) का मतलब है किसी task को बार-बार करना। जब हमें कोई काम कई बार करना होता है, जैसे किसी array के हर element को check करना, तो हम loop का इस्तेमाल करते हैं। Loops हमारी मदद करते हैं ताकि हम एक ही code को बार-बार ना लिखें, बल्कि एक structure का पालन करते हुए उसे multiple times execute कर सकें।
For Loop for Repetition in JavaScript
For loop का इस्तेमाल किसी task को specific number of times repeat करने के लिए किया जाता है। यह तीन भागों में काम करता है: initialization, condition, और increment/decrement।
- Initialization: यह वो point है जहाँ loop की शुरुआत होती है, जैसे कि variable की value को set करना।
- Condition: जब तक यह condition true रहती है, loop चलता रहता है। जैसे ही यह false होती है, loop बंद हो जाता है।
- Increment/Decrement: हर iteration के बाद value को बढ़ाना या घटाना होता है, ताकि condition eventually false हो जाए।
Example of For Loop in JavaScript:
for (let i = 0; i < 5; i++) { console.log(i); }
यह loop 0 से लेकर 4 तक numbers को print करेगा।
While Loop for Repetition in JavaScript
While loop का उपयोग तब किया जाता है जब हमें पता नहीं होता कि loop कितनी बार चलेगा, लेकिन हमें एक condition चाहिए होती है जो loop को चलाने के लिए true हो। जब तक condition true रहती है, loop चलता रहता है।
- Condition: यह loop के पहले check होती है। यदि condition true है, तो loop execute होता है।
- Stopping Condition: जब condition false हो जाती है, loop बंद हो जाता है।
Example of While Loop in JavaScript:
let i = 0; while (i < 5) { console.log(i); i++; }
यह loop भी 0 से लेकर 4 तक numbers को print करेगा, लेकिन इसकी condition पहले check होती है।
Do-While Loop for Repetition in JavaScript
Do-While loop, while loop की तरह ही काम करता है, लेकिन इसमें फर्क है कि यह कम से कम एक बार execute जरूर होता है, चाहे condition true हो या false। पहले एक बार statement execute होता है, फिर condition check होती है।
- Condition check: यह loop के अंत में होती है, इसलिए कम से कम एक बार code execute होता है।
Example of Do-While Loop in JavaScript:
let i = 0; do { console.log(i); i++; } while (i < 5);
यह loop भी 0 से लेकर 4 तक numbers को print करेगा, लेकिन condition को अंत में check किया जाता है।
For-In Loop for Repetition (Object Iteration) in JavaScript
For-In loop का इस्तेमाल objects के properties को iterate करने के लिए किया जाता है। यह loop object के हर property पर चलता है और उसका नाम और value retrieve करता है।
- It is particularly useful for objects.
Example of For-In Loop in JavaScript:
const person = { name: "John", age: 30, city: "New York" }; for (let key in person) { console.log(key + ": " + person[key]); }
यह loop object के properties को print करेगा, जैसे name, age, और city।
For-Of Loop for Repetition (Array Iteration) in JavaScript
For-Of loop का इस्तेमाल array के elements को iterate करने के लिए किया जाता है। यह loop array के हर item पर चलता है और उसे easily access करता है।
- It simplifies iteration over arrays or other iterable objects.
Example of For-Of Loop in JavaScript:
const numbers = [10, 20, 30, 40, 50]; for (let number of numbers) { console.log(number); }
यह loop array के हर element को print करेगा।
Nested Loops for Repetition in JavaScript
Nested loops का मतलब है loops के अंदर loops का होना। जब हमें multi-dimensional data, जैसे 2D arrays, handle करना होता है, तब nested loops का इस्तेमाल किया जाता है।
- It is useful for working with matrices or arrays of arrays.
Example of Nested Loops in JavaScript:
const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; for (let i = 0; i < matrix.length; i++) { for (let j = 0; j < matrix[i].length; j++) { console.log(matrix[i][j]); } }
यह nested loop 2D array के हर element को print करेगा।
FAQs
Loops in JavaScript are used to execute a block of code repeatedly based on a condition. They help reduce code repetition, making it easier to handle repetitive tasks such as iterating over arrays or objects.
The main difference is in how the condition is checked. In a "for" loop, the condition is checked at the beginning and executed a set number of times, while in a "while" loop, the condition is checked before every iteration and can run indefinitely if the condition is true.
A "do-while" loop is used when you want the block of code to run at least once, regardless of the condition. The condition is checked after the code execution, which guarantees that the loop runs at least one time.
The "for-in" loop is used for iterating over the properties of an object. It allows you to access each property name and its corresponding value in the object.
The "for-of" loop is used for iterating over iterable objects like arrays, strings, and maps, whereas the "for-in" loop is used for iterating over the properties of an object. "for-of" gives the values directly, while "for-in" gives the property names.
Yes, JavaScript supports nested loops. Nested loops are loops inside other loops, and they are commonly used to handle multi-dimensional arrays or matrices where each loop handles different levels of data.