Using the Continue Statement in JavaScript Loops
📖 Scenario: You are working on a simple program that processes a list of numbers. Sometimes, you want to skip certain numbers based on a condition and continue with the rest.
🎯 Goal: Build a JavaScript program that uses the continue statement inside a for loop to skip even numbers and print only odd numbers from a list.
📋 What You'll Learn
Create an array called
numbers with the values [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].Create a variable called
skipEven and set it to true.Use a
for loop to go through each number in numbers.Inside the loop, use an
if statement to check if skipEven is true and the current number is even.If the number is even and
skipEven is true, use the continue statement to skip printing that number.Print only the numbers that are not skipped.
💡 Why This Matters
🌍 Real World
Skipping certain items in a list is common when filtering data, such as ignoring invalid entries or focusing on specific values.
💼 Career
Understanding how to control loops with continue helps in writing efficient code for data processing, web development, and automation tasks.
Progress0 / 4 steps