Someone recently asked how to use the continue statement in a for loop in TypeScript. The continue
statement can help you control the flow of your loops more effectively, allowing you to skip certain iterations based on specific conditions. In this tutorial, I will explain how to use the continue statement in TypeScript for loops with some examples.
What is the Continue Statement in TypeScript Loops?
The continue
statement in TypeScript is used to skip the current iteration of a loop and proceed directly to the next iteration. This can be particularly useful when you need to bypass certain elements in an array or collection based on a condition.
Using Continue in a For Loop
Let’s start with a basic example. Suppose we have an array of city names from the USA, and we want to skip processing any city that starts with the letter “S”.
let cities: string[] = ["New York", "San Francisco", "Los Angeles", "Chicago", "Seattle"];
for (let i = 0; i < cities.length; i++) {
if (cities[i].startsWith("S")) {
continue;
}
console.log(`Processing city: ${cities[i]}`);
}
In this example, the loop will skip over “San Francisco” and “Seattle” and will only process “New York”, “Los Angeles”, and “Chicago”. The continue
statement effectively skips the current iteration whenever the city’s name starts with “S”.
Here is the output in the screenshot below:

Check out Do-While Loop in TypeScript
Using Continue in a For…of Loop
TypeScript also supports the for...of
loop, which is a more modern and readable way to iterate over arrays. Here’s how you can use the continue
statement in a for...of
loop:
let cities: string[] = ["New York", "San Francisco", "Los Angeles", "Chicago", "Seattle"];
for (let city of cities) {
if (city.startsWith("S")) {
continue;
}
console.log(`Processing city: ${city}`);
}
This example achieves the same result as the previous one but uses the for...of
loop for better readability and simplicity.
Here is the exact output of the above TypeScript code.

Read How to Break Out of Loops in TypeScript?
Using Continue in a While Loop
The continue
statement can also be used within a while
loop. Here’s an example where we process a list of states, skipping those that start with “N”:
let states: string[] = ["California", "Nevada", "New York", "Texas", "New Jersey"];
let index = 0;
while (index < states.length) {
if (states[index].startsWith("N")) {
index++;
continue;
}
console.log(`Processing state: ${states[index]}`);
index++;
}
In this case, the loop will skip “Nevada”, “New York”, and “New Jersey”, processing only “California” and “Texas”.
Here is the exact output in the screenshot below:

Check out How to Break Out of a forEach Loop in TypeScript?
Continue Statement in TypeScript For Loops Examples
Now, let me show you some examples of the continue statement in TypeScript for loops.
Filtering User Data
Imagine you have a list of user names, and you need to process them but skip any names that start with “A”. Here’s how you can do it:
let users: string[] = ["Alice", "Bob", "Andrew", "Michael", "Amanda"];
for (let user of users) {
if (user.startsWith("A")) {
continue;
}
console.log(`Processing user: ${user}`);
}
This code will skip “Alice”, “Andrew”, and “Amanda”, processing only “Bob” and “Michael”.
Skipping Invalid Entries
You might also encounter scenarios where you need to skip invalid entries in a dataset. For example, if you have a list of product codes and you want to skip any invalid codes (e.g., those that are empty or null):
let productCodes: (string | null)[] = ["ABC123", null, "XYZ789", "", "LMN456"];
for (let code of productCodes) {
if (!code) {
continue;
}
console.log(`Processing product code: ${code}`);
}
This loop will skip null
and empty strings, processing only the valid product codes.
Conclusion
The continue statement in TypeScript is used for controlling the flow of loops. By skipping specific iterations based on conditions, you can make your code more efficient and easier to read. In this tutorial, I explained how to use the continue statement in TypeScript for loops.
You may also like the following tutorials:
- TypeScript forEach Loop with Index
- How to Use for…of Loops in TypeScript?
- How to Use for…in Loops in TypeScript?
- How to Use the For Loop in TypeScript?
I’m Bijay Kumar Sahoo, and I am honored to be awarded the Microsoft MVP. With over 18 years of experience in the IT industry, I got a chance to work on SharePoint Framework (SPFx) development, TypeScript, React, JavaScript, etc. My journey has taken me through esteemed organizations such as TCS, HP, and KPIT, where I have honed my skills and expanded my expertise. Check out more about me here.