DiscoveryVIP / Interactive learningJAVASCRIPT DISCOVERY ACADEMY

FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION

Using break and continue

Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.

Predict → Experiment → Build → Test → Explain

Educator & demo scripts

Ready. Start with one instruction.

Loading the learning workspace…

Printable lesson notes · Using break and continue

continue skips the remainder of the current loop body and proceeds to the next iteration. break exits the loop entirely. They affect control flow, so place them near the condition they express. This example skips even values while keeping later odd ones. Replacing continue with break would stop at the first even number and lose valid later values.

Objectives

Worked example 1 · See the idea

const end = 5;
for (let i = 1; i <= end; i++) {
  if (i % 2 === 0) continue;
  console.log(i);
}

Expected result

1
3
5

The even value is skipped, but the loop continues and can still print later odds.

Line-by-line explanation

Line 1
const end = 5;

Declare end with const. The binding cannot be reassigned. The expression after = supplies its initial value; = is assignment, not an equality comparison.

Line 2
for (let i = 1; i <= end; i++) {

Initialize the counter once, test its condition before each iteration, and update it after the body. Track the boundary carefully.

Line 3
if (i % 2 === 0) continue;

Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.

Line 4
console.log(i);

Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.

Line 5
}

Close the current instruction block.

Worked example 2 · Change the value

const end = 2;
for (let i = 1; i <= end; i++) {
  if (i % 2 === 0) continue;
  console.log(i);
}

Expected result

1

The even value is skipped, but the loop continues and can still print later odds.

Line-by-line explanation

Line 1
const end = 2;

This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.

Line 2
for (let i = 1; i <= end; i++) {

Initialize the counter once, test its condition before each iteration, and update it after the body. Track the boundary carefully.

Line 3
if (i % 2 === 0) continue;

Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.

Line 4
console.log(i);

Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.

Line 5
}

Close the current instruction block.

Worked example 3 · Explore a boundary

const end = 1;
for (let i = 1; i <= end; i++) {
  if (i % 2 === 0) continue;
  console.log(i);
}

Expected result

1

The even value is skipped, but the loop continues and can still print later odds.

Line-by-line explanation

Line 1
const end = 1;

This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.

Line 2
for (let i = 1; i <= end; i++) {

Initialize the counter once, test its condition before each iteration, and update it after the body. Track the boundary carefully.

Line 3
if (i % 2 === 0) continue;

Choose a branch according to this condition. Only the selected path runs for this input; inspect the exact comparison and its boundary.

Line 4
console.log(i);

Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.

Line 5
}

Close the current instruction block.

Independent exercise

Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.

const end = 5;
for (let i = 1; i <= end; i++) {
  if (i % 2 === 0) break;
  console.log(i);
}
Reference solution
const end = 5;
for (let i = 1; i <= end; i++) {
  if (i % 2 === 0) continue;
  console.log(i);
}

Hints

  1. Compare the observed result with the expected result. Read the relevant source line aloud.
  2. The operation to inspect is: continue
  3. Use continue in place of break. Rerun and explain why the change works.

Professional tip and common mistake

Choose break when the entire search is finished, not when only one item should be ignored.

Reflect and extend

Which operation changed the result, why did it work, and what would you test next?

Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.

Related: Previous lesson Next lesson

Course syllabus and printable lesson notes

100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.

1. Your First Steps into Programming
  1. What JavaScript is and what you can build with it
  2. Understanding instructions, code, and programs
  3. Running JavaScript and using the console
  4. Understanding syntax, comments, and basic code structure
  5. Building your first interactive JavaScript experiment
2. VARIABLES AND INFORMATION
  1. Understanding variables as containers for information
  2. Declaring variables with let
  3. Using const and understanding reassignment
  4. Naming variables and writing readable code
  5. Changing values and tracking program state
3. JAVASCRIPT DATA TYPES
  1. Understanding strings
  2. Understanding numbers
  3. Understanding Boolean values
  4. Understanding null and undefined
  5. Identifying and converting data types
4. OPERATORS AND CALCULATIONS
  1. Basic arithmetic operations
  2. Operator precedence and parentheses
  3. Assignment and compound assignment
  4. Increment, decrement, remainder, and practical calculations
  5. Building useful mathematical expressions
5. WORKING WITH TEXT
  1. Creating and combining strings
  2. Understanding template literals
  3. Measuring and accessing string characters
  4. Searching, slicing, replacing, and transforming text
  5. Building practical text-processing applications
6. MAKING DECISIONS
  1. Comparison operators
  2. Understanding equality and strict equality
  3. Writing if and else statements
  4. Working with else if and multiple conditions
  5. Logical operators and the ternary operator
7. REPEATING INSTRUCTIONS
  1. Understanding repetition and loops
  2. Writing for loops
  3. Writing while and do...while loops
  4. Using break and continue
  5. Nested loops and repeated patterns
8. FUNCTIONS AND REUSABLE CODE
  1. Understanding what functions do
  2. Defining and calling functions
  3. Using parameters and arguments
  4. Returning values
  5. Function expressions and arrow functions
9. SCOPE AND EXECUTION
  1. Understanding global and local scope
  2. Block scope with let and const
  3. Understanding the call stack
  4. Nested functions and introductory closures
  5. Organizing programs into smaller responsibilities
10. ARRAYS AND COLLECTIONS
  1. Creating and understanding arrays
  2. Accessing elements using indexes
  3. Adding, removing, and updating elements
  4. Iterating over arrays
  5. Searching, sorting, and organizing arrays
11. MODERN ARRAY METHODS
  1. Transforming data with map()
  2. Selecting data with filter()
  3. Finding elements with find() and related methods
  4. Accumulating values with reduce()
  5. Combining array methods into transformation pipelines
12. OBJECTS AND STRUCTURED DATA
  1. Understanding JavaScript objects
  2. Accessing and changing object properties
  3. Creating nested objects and arrays of objects
  4. Understanding references and mutation
  5. Using destructuring, spread syntax, and object methods
13. JAVASCRIPT AND WEB PAGES
  1. Understanding HTML, CSS, JavaScript, and the browser
  2. Understanding the Document Object Model
  3. Selecting elements with JavaScript
  4. Updating content, attributes, and CSS classes
  5. Creating, inserting, and removing elements
14. EVENTS AND USER INTERACTION
  1. Understanding browser events
  2. Working with click and input events
  3. Handling forms and user submissions
  4. Understanding event objects and event propagation
  5. Building accessible interactive interfaces
15. PRACTICAL WEB APPLICATIONS
  1. Understanding application state
  2. Rendering data into the interface
  3. Building reusable interface components
  4. Saving information with localStorage
  5. Combining state, events, and persistence
16. ERRORS, DEBUGGING, AND TESTING
  1. Understanding syntax, runtime, and logic errors
  2. Reading error messages and console output
  3. Debugging with breakpoints and state inspection
  4. Handling exceptions with try and catch
  5. Writing basic tests and checking edge cases
17. ASYNCHRONOUS JAVASCRIPT
  1. Understanding synchronous and asynchronous code
  2. Timers and delayed execution
  3. Understanding promises
  4. Using async and await
  5. Understanding the event loop, tasks, and microtasks
18. APIS AND EXTERNAL DATA
  1. Understanding APIs and HTTP
  2. Working with JSON
  3. Retrieving information using fetch()
  4. Handling loading states, errors, and unsuccessful requests
  5. Building an interactive application using API data
19. MODERN JAVASCRIPT DEVELOPMENT
  1. Organizing code into modules
  2. Understanding classes and object-oriented programming
  3. Writing maintainable and reusable JavaScript
  4. Understanding browser performance and efficient updates
  5. Building responsibly with AI-assisted coding
20. BECOMING AN INDEPENDENT DEVELOPER
  1. Turning an application idea into requirements
  2. Planning features, data, and interface behavior
  3. Building and testing an initial implementation
  4. Debugging, improving, and extending an application
  5. Completing and publishing your JavaScript capstone project