Home 〉 Javascript Tutorials 〉 JavaScript Loops: Mastering for, while, do...while, for...in, for...of, and forEach
JavaScript Loops: Mastering for, while, do...while, for...in, for...of, and forEach !
Are you struggling to understand how loops work in JavaScript? Loops are fundamental in programming, allowing you to execute a block of code multiple times efficiently. This tutorial covers various types of loops in JavaScript, including for, while, do...while, for...in, for...of, and forEach. Each loop type is explained with syntax and practical examples to help you grasp their usage. By the end of this guide, you'll be able to choose the appropriate loop for different scenarios in your code. Let's dive into the world of JavaScript loops and enhance your programming skills.
Loops in JavaScript are used to perform repeated tasks based on a condition. They are essential for tasks like iterating over arrays, performing operations multiple times, and automating repetitive processes. Understanding different loop types helps in writing efficient and clean code.
The for loop is used when the number of iterations is known. It consists of three parts: initialization, condition, and increment/decrement.
<!DOCTYPE html>
<html>
<body>
<p id="js-loop"></p>
<script>
for (let i = 0; i < 5; i++) {
document.getElementById('js-loop').innerHTML += i + '<br>';
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This loop will print numbers from 0 to 4.
The while loop executes a block of code as long as the specified condition is true.
<!DOCTYPE html>
<html>
<body>
<p id="js-loop1"></p>
<script>
var i = 0;
while (i < 5) {
document.getElementById('js-loop1').innerHTML += i + '<br>';
i++;
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This loop also prints numbers from 0 to 4.
The do...while loop is similar to the while loop, but it executes the block of code at least once before checking the condition.
<!DOCTYPE html>
<html>
<body>
<p id="js-loop2"></p>
<script>
var i = 0;
do {
document.getElementById('js-loop2').innerHTML += i + '<br>';
i++;
} while (i < 5);
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This loop ensures the code runs at least once, printing numbers from 0 to 4.
The for...in loop is used to iterate over the enumerable properties of an object.
<!DOCTYPE html>
<html>
<body>
<p id="js-loop3"></p>
<script>
var person = {fname:"John", lname:"Doe", age:25};
for (let key in person) {
document.getElementById('js-loop3').innerHTML += key + ": " + person[key] + '<br>';
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This loop will print each property and its value in the person object.
The for...of loop is used to iterate over iterable objects like arrays, strings, etc.
<!DOCTYPE html>
<html>
<body>
<p id="js-loop4"></p>
<script>
var numbers = [1, 2, 3, 4, 5];
for (let num of numbers) {
document.getElementById('js-loop4').innerHTML += num + '<br>';
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This loop will print each number in the numbers array.
The forEach method executes a provided function once for each array element.
<!DOCTYPE html>
<html>
<body>
<p id="js-loop5"></p>
<script>
var fruits = ["apple", "banana", "cherry"];
fruits.forEach(function(item, index) {
document.getElementById('js-loop5').innerHTML += index + ": " + item + '<br>';
});
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This will print each fruit with its index.
Understanding different loops in JavaScript is crucial for writing efficient code. Each loop serves a specific purpose and choosing the right one depends on the task at hand. Practice using these loops to become proficient in controlling the flow of your programs.
A for loop is typically used when the number of iterations is known, whereas a while loop is used when the number of iterations is unknown and depends on a condition.
Yes, break is used to exit a loop prematurely, and continue skips the current iteration and moves to the next one.
Yes, loops can be nested within each other to perform complex iterations, such as iterating over multi-dimensional arrays.
The forEach loop is used to execute a function on each element of an array, providing a concise way to iterate over arrays.
Use for...in to iterate over object properties and for...of to iterate over iterable objects like arrays and strings.