Infinite loop crash

While I was learning basic JavaScript course, specifically “Count Backwards With a For Loop” lesson, I was trying to play around with the code, and I wondered what kind of error will show up if i run an infinite loop, so i changed the code little bit:

// Setup
const myArray = [];

// Only change code below this line
for (let i = 10; i > 1; i += 2) {

I expected some kind of error, but surprisingly that completely crashed my laptop!
After the crash is fixed the console had printed an array with numbers from ten to 700,000!
Shouldn’t infinite loops just throw an error instead of running until crash?

How can the code know it’s infinite unless trying to execute it? In short, no.

At most, if there is an infinite loop protection of some kind, what it does is stop a loop after a certain amount of time, still not an error.

Some development environment will throw a warning if your code will cause an infinite loop, but as for why it doesn’t throw an error is because technically there’s nothing wrong with your code in the computers eye, just that the task you have given the computer has no point where it will terminate.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.