Search code examples
javascriptloopscrashinfinite-loop

JS - How does this loop crash the browser?


I wrote my first JavaScript code in Microsoft Edge's JS console:

var times = 10;
for(var i = 0; i < times; times++)
{
    console.log(times);
}

Soon after, it make the browser become unresponsive and crash.

Why and How?

Granted it is an infinite loop. But how does storing the value of times and incrementing it continuosly crashes it. Is holding the value of a few variables that taxing?

After all there are a million other calculations being performed continously in the computers like time and gui features of the system. They don't cause crashes.

I am definitely missing something about the actual internal workings that produce this crash.

To summarize the question: How does above loop crash the browser?

Explanation of internal mechanism needed.


Solution

  • var times = 10;
    for(var i = 0; i < times; i++)
    {
        console.log(i);
    }
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    You were incrementing the "wrong" variable.
    By incrementing times, i will never "reach" it.