Freeze your browser by running JS

Cover Image for Freeze your browser by running JS

Javascript, as you already might've known was descended from C family of programming languages . So Javascript obviously inherited some of the coolest and powerful features as its predecessor. One of them is While loop. And another is For Loop.

While loop

While loop as in other C based languages holds a special place in Javascript too. Let's see the syntax

while (condition is true) {
    //Do Something
    //when condition becomes false break out of loop
}

The syntax is pretty simple. However when the loop becomes infinite, the browser just freezes

Let us see an example

n = 1

while (n > 0) {
    n++
    console.log(n)
}

Running this while loop will freeze your browser (depends on your RAM and CPU) and you will have trouble closing the tab

For Loop

For loop starts with an initial value, the condition to satisfy, the steps to follow(increment or decrement)

for (let i=initialValue; i > condition; i steps) {
    console.log(i)
}

Running the below code will also freeze your browser

for (let i=1; i > 0; i++) {
    console.log(i)
}

This is one way of learning about loops in Javascript.

That's it folks!!!