Using Javascript Continue to Skip Code Evaluation in For Loops

Ole Ersoy
Oct 1, 2022
Image by experimentMR from Pixabay

Scenario

We have an array of numbers .

const arr: number[] = [1, 2, 3, 4, 5];

We only want to log the even numbers.

Approach

The modulo expression i % 2 will return zero for all even numbers, and and the continue statement causes the for loop to begin the next iteration, skipping the console.log(i) statement.

for (let i of arr) {
if (i % 2) {
continue;
}
console.log(i);
}

Demo

--

--