Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Continue Statement in JavaScript

The JavaScript continue statement skips the current iteration immediately and continues to the next iteration of the loop. Unlike the break statement, the continue statement does not exit the loop. It only skips the current iteration.

Syntax

                            
continue [label];
  
                        
                            
                        

Where the label is optional. We use it when we have a nested loop to correctly identify the loop

Continue with a while loop

In the following while loop example, we use the continue statement to skip the iteration if the value of i is 3. Because of this, the value 3 is not printed on the console. The loop does not stop but continues to the next iteration.

                            
 var i = 0;
 
 while (i < 5) {
   i++;
   if (i === 3) {
     continue;
   }
   console.log(i);
 }
  
  
  
 ** Console **
 1
 2
 4
 5
  
 

 
 
                            
                        
                            
                        

Continue with a for loop

In thisfor loop example also skips the rest of the loop, when the value of i is 3.

                            
for (let i = 0; i < 5; i++) {
  if (i == 3) continue;
  console.log(i);
}
 
** Console **
0
1
2
4
5
 

 
 
                            
                        
                            
                        

Continue statement in nested loops

In the following nested for loop example, the continue statement skips the inner loop, but not the outer loop.

                            
for (let i = 0; i < 4; i++) {
  for (let j = 0; j < 4; j++) {
    if (j == 2) continue;
    console.log(i + " " + j);
  }
}
 
*** Console ***
0 0
0 1
0 3
1 0
1 1
1 3
2 0
2 1
2 3
3 0
3 1
3 3
 
                            
                        
                            
                        

To exit from the outer loop, we make use of the labeled statement

In the following examples, we label the outer loop as outerloop and inner loop as innerloop. The continue statement now skips the outer loop

                            
outerloop: for (let i = 0; i < 4; i++) {
  innerloop: for (let j = 0; j < 4; j++) {
    if (j == 2) continue outerloop;
    console.log(i + " " + j);
  }
}
 
*** Console ***
0 0
0 1
1 0
1 1
2 0
2 1
3 0
3 1