DONATE  
MENU
MYSQL
JAVASCRIPT
CSS
AJAX
JSON
PHP
HTML

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Loops » Break & Continue
Share Tweet WhatsApp

Break & Continue

The break and continue keywords are two important keywords that facilitates both conditional testing and looping. break Keyword The break keyword enables us to totally "jump" out of a loop or conditional testing. That is, whenever a loop or conditional testing is executing and meets the break keyword, that signifies the end of that loop or conditional testing. You will use the break keyword whenever you are looping for example and on meeting a condition you are satisfied with, you decide to end the loop. You therefore use the keyword to end it.
<script>
var num = 25;

for (var i = 2; i < num; i++){
if (num % i == 0){
alert(i+" is the smallest number that can divide "+num);
break;
}
}
</script>

Copy PRACTICE IT

Above, we are looping from the number "2" to "24" with the aim of finding the smallest number that can divide "25". Once we find a number that can do just that, we end the loop with the break keyword. continue Keyword The continue keyword on the other hand allows us to "jump" out of a particular loop execution and "continue" with the next execution. It does not end the whole loop rather it ends a particular loop execution and goes to the next. For example, assuming a loop was suppose to execute 5 times, we can use the continue keyword to stop the execution of one of the loops so that the loop executes only 4 times instead of 5.
<script>
var num = 4;

for (var i = 1; i <= num; i++){
if (i == 3){
continue; //When i becomes 3, the loop will jump to the next execution
}
alert("This is execution "+i);
}
</script>

Copy PRACTICE IT

Above, we are looping from the number "1" to "4" but once we get to "3", we jump over its execution and move to the next execution which is "4". SUMMARY
  1. The break and continue keywords are two very important keywords that helps us in conditional testing/decision making and looping.
  2. The break keyword enables us to end a loop whenever a condition is met.
  3. The continue keyword enables us to end a particular execution of a loop while it moves to the next execution.
Take a short quiz on Break & Continue.
Ask a question or tell us if Break & Continue is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode