Using Javascript Continue to Skip Code Evaluation in For Loops

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

--

--

Founder of Firefly Semantics Corporation

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation