There are a number of different ways to loop or iterate in JavaScript: for
, while
, do ... while
For Loops
If the embed above does not work here is a link to the full version of the video
A for
loop has the following general structure:
for (initialisation; condition; final - expression) {
// ... code to execute on each iteration
}
- The initialisation is where we set up the variable used in iteration (a counter for instance)
- The condition is the boolean expression we check on each iteration to check whether the loop should continue
- The final-expression is code that is always executed last of all on each iteration (usually to increment/decrement the counter)
for (let i = 0; i < 10; i++) {
console.log(i);
}
While loops
If the embed above does not work here is a link to the full version of the video
A while
loop has the following general structure:
while(boolean condition) {
// ... code to execute on each iteration
}
The block of code within the loop will be run while the boolean condition evaluates to true
. It is very easy to cause an infinite loop with a while loop as you can easily forget to ensure that the boolean condition will change at some point.
break
We can use the break
keyword to exit a loop
for (let i = 0; i < 10; i++) {
console.log(i);
if (i > 5) {
break;
}
}
continue
We can use the continue
keyword to stop the current iteration and skip to the next
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}
Code examples
There are a set of code examples that accompany this course showing the use of JavaScript. (Right click and open in a new window/tab) if you’re viewing this on Learning Central.
- Variable Types [HTML] [Demo]
- Numerical Operators [HTML] [Demo]
- Booleans [HTML] [Demo]
- Looping [HTML] [Demo]
Next: Writing JavaScript
Previous: Branching and Boolean Expressions