Introduction
In JavaScript, an infinite loop is a loop that continues to run indefinitely without ever terminating. This can happen when the condition that is being tested in the loop never evaluates to false, or when the loop is missing a crucial element such as a stop condition or a means to increment the loop variable.
In this article, we will discuss how to create an infinite loop using the while and for loops and how to avoid them.
Create JavaScript infinite loop
Method-1: Using a while loop
To have any infinite, the condition for the loop needs to always be true. In a while loop, we can simply pass the true
keyword as below
while (true) {
console.log(1);
}
Output
1
1
1
1
1
The code above will continue to return 1
for as long as possible until the program crashes.
Here is another example but instead of passing the true
value, we pass a condition that will always be true. In the code below, we will have an infinite loop because we don’t have an increment operation for the variable i
, and therefore i < 10
will always be true.
let i = 1;
while (i < 10) {
console.log(i);
}
Output
1
1
1
1
Method-2: Using for loop
For for
loops, it's pretty much the same. We can pass the true
value in place of a conditional statement. Here, the value index
increases, but there is no stop condition and so the loop goes on forever.
let i = 1;
for (let index = 0; true; index++) {
console.log(index);
}
Output
0
1
2
3
4
5
6
7
8
9
10
11
12
13
Avoid Infinite Loop in JavaScript
Infinite loops can be a problem because they can cause the browser to freeze or crash, or they can consume large amounts of processing power and memory, potentially impacting the performance of other tasks on the system.
To avoid creating infinite loops in JavaScript, it's important to make sure that the loop is properly structured and that it includes all the necessary elements. This includes a stop condition that will cause the loop to terminate once it is met, as well as a means to increment the loop variable.
Here is an example of a properly structured "for" loop in JavaScript:
for (let i = 0; i < 10; i++) {
console.log(i)
}
Output
0
1
2
3
4
5
6
7
8
9
In this example, the loop will continue to run as long as the variable i
is less than 10. Each time the loop runs, the i
variable will be incremented by 1, eventually reaching 10 and causing the loop to terminate.
If the loop is missing any of these elements, it can result in an infinite loop. For example, the following code will create an infinite loop because it is missing the means to increment the loop variable:
for (let i = 0; i < 10;) {
console.log(i)
}
In this case, the loop will continue to run indefinitely because the "i" variable is never incremented, so it will always be less than 10.
Summary
To avoid creating infinite loops in JavaScript, it's important to make sure that your loops are properly structured and include all the necessary elements. This includes a stop condition that will eventually be met, as well as a means to increment the loop variable. By following these guidelines, you can avoid creating infinite loops that can cause problems for your code.
References
Loops and iteration - JavaScript | MDN (mozilla.org)
Further Reading
How do you stop an infinite loop in Javascript? - Stack Overflow