Loops
for, for...of, for...in and while — and which one to reach for.
The for statement#
JavaScript has two methods for running the same code several times. It is mainly used for iterating over arrays or objects. Let’s see an example:
let i;
for (i = 0; i < 3; i = i + 1)
{
console.log(i);
}This will print out the following:
0
1
2The for statement in JavaScript has the same syntax as in Java and C. It has three parts:
- Initialization - Initializes the iterator variable
i. In this example, we initializeito 0.
- Condition - As long as the condition is met, the loop continues to execute. In this example, we check that
iis less than 3.
- Increment - A directive which increments the iterator. In our case, we increment it by 1 on every loop.
We can also write a shorter notation for the statement by inserting the variable definition inside the for loop and incrementing using the ++ operator.
for (let i = 0; i < 3; i++)
{
console.log(i);
}To iterate over an array and print out all of its members, we usually use the for statement. Here’s an example:
let myArray = ["A", "B", "C"];
for (let i = 0; i < myArray.length; i++)
{
console.log("The member of myArray in index " + i + " is " + myArray[i]);
}This prints out the contents of the array:
The member of myArray in index 0 is A
The member of myArray in index 1 is B
The member of myArray in index 2 is CNotice that we used the length property of an array, which returns the number of members in the array, so we know when to stop iterating.
The while statement#
The while statement is a more simple version of the for statement which checks if an expression evaluates to true and runs as long as it says true.
For example:
let i = 99;
while (i > 0)
{
console.log(i + " bottles of beer on the wall");
i -= 1;
}break and continue statements#
The break statement allows to stop the execution of a loop. For example, we can create a loop that loops forever using while(true) and use the break statement to break inside the loop instead by checking that a certain condition was met.
let i = 99;
while (true)
{
console.log(i + " bottles of beer on the wall");
i -= 1;
if (i == 0)
{
break;
}
}The continue statement skips the rest of the loop and jumps back to the beginning of the loop. For example, if we would want to print only odd numbers using a for statement, we can do the following:
for (let i = 0; i < 100; i++)
{
// check that the number is even
if (i % 2 == 0)
{
continue;
}
// if we got here, then i is odd.
console.log(i + " is an odd number.");
}Try it
Expected output
let i = 99;
while (true)
{
console.log(i + " bottles of beer on the wall");
i -= 1;
if (i == 0)
{
break;
}
}Solution
for (let i = 0; i < 100; i++)
{
// check that the number is even
if (i % 2 == 0)
{
continue;
}
// if we got here, then i is odd.
console.log(i + " is an odd number.");
}Get the TypeScript agent pack
A battle-tested AGENTS.md, the review checklist, and the failure-mode cheat sheet for TypeScript. One email, then occasional updates when the tooling shifts. No course pitch.
AGENTS.md now — no email needed.