losatx.blogg.se

While loop js
While loop js







Note! In a do-while loop the body will be executed at least once. In a do-while loop we first execute the loop body and then check the condition at the end. In a while loop we first check the condition and then execute the loop body. The difference between a while and a do-while loop is: If the condition of the do-while loop is satisfied then the body of the do-while loop is executed otherwise, skipped. We use the do and while keyword to create a do-while loop. When the value of i becomes 11, we exit the while loop.Īnother type of while loop is the do-while loop. This process is repeated as long as i <= 10. Next we increment the value of variable i by 1 using the increment operator. Inside the loop we print the "Hello World" message in the browser console.

while loop js

If this is true then we enter into the while loop. In the above code we have a variable i which is set to 1.

while loop js

In the following example we have a while loop that helps in printing out "Hello World" 10 times in the browser console. If the condition of the while loop is satisfied then the body of the while loop is executed otherwise, skipped. We use the while keyword to create a while loop. Though, the first option looks better than the second. We use loop to avoid retyping a piece of code when we want to repeat it multiple times.Įxample: We can use loop and print "Hello World" 10 times using one console.log() or we can write the console.log() 10 times and get the job done. In this tutorial we will learn about JavaScript while loop.Ī loop is a piece of code that allows us to repeat a code multiple time based on some condition.









While loop js