Welcome To My Web Site

12/14/2021

JavaScript Loops

Loops

In JavaScript we learned about ways to iterate and repeat lines of code until conditions are met. There are many different types of loops in this post I will be cover For Loops and While Loops.

While Loops


While loops will continue to loop until the condition set in their parameters are true. The condition set MUST be a boolean expression meaning it must evaluate to TRUE or FALSE

while (boolean condition){
	//code here will repeat until condition evaluates to false
}
				

Here is an example of a while loop:

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

For Loop

While loops look until the condition in their parameters are evaluated to be false For Loops repeat for a set period of time.

for(initalize variable; boolean expression using variable; increment the variable){
	//code here will repeat until boolean expression returns false
}
				

Here is an example of a for loop:

for(var x = 0; x < 30; x++){
	console.log(x);
}