while Loops
A while loop keeps running as long as its condition stays true — useful when you don't know in advance how many times you'll need to loop.
main.ts
Try it yourself →let count: number = 0;
while (count < 5) {
console.log(count);
count++;
}Output
0 1 2 3 4
Exiting early with break
break immediately exits the loop, regardless of the condition — the same as in JavaScript.
main.ts
Try it yourself →let n: number = 0;
while (true) {
if (n === 3) {
break;
}
console.log(n);
n++;
}Output
0 1 2
Next: array methods — map, filter, and reduce, with TypeScript inferring types through every step.
Practice exercise
Write a while loop that counts down from 5 to 1, printing each number, then stops (do not print 0).