for...of Loop (एरे और स्ट्रिंग के लिए)
हिंदी व्याख्या:
for...of loop का उपयोग किसी array, string आदि की values को एक-एक करके निकालने के लिए किया जाता है।
यह direct value देता है।
English Explanation: for...of is used to loop through values of iterable objects like arrays and strings.
let fruits = ["Apple", "Banana", "Mango"];
for (let fruit of fruits)
{
document.write(fruit);
}
Output:-
Apple
Banana
Mango
अगर for...of loop के अंदर ही केवल Apple print कराना हो, तो condition लगा सकते हैं:
let fruits = ["Apple", "Banana", "Mango"];
for (let fruit of fruits)
{
if (fruit == "Apple")
{
document.write(fruit);
}
}
Without for..of loop:-
let fruits = ["Apple", "Banana", "Mango"];
{
document.write(fruits[0] + "<br>");
document.write(fruits[1] + "<br>");
document.write(fruits[2] + "<br>");
}
Output:-
Apple
Banana
Mango