JavaScript: Continue Statement - TechOnTheNet
Maybe your like
- Home
- JavaScript
JavaScript: Continue Statement This JavaScript tutorial explains how to use the continue statement with syntax and examples.
Description
In JavaScript, the continue statement is used when you want to restart a new iteration of a loop. This statement can be used in a while loop, for loop or for-in loop. If you try using the continue statement in other loop types, you might get unexpected results.
When JavaScript executes the continue statement, any remaining code left in the current iteration of the loop body is skipped. The code will resume execution at the start of the loop (as a new iteration of the loop).
You can also use a continue statement to execute a labeled statement.
Syntax
The syntax for the continue statement in JavaScript is:
continue [label_name];Parameters or Arguments
label_name Optional. An identifier name (or label name) for a statement.Note
- You use the continue statement to restart a loop such as the while loop, for loop or for-in loop.
- If there are nested loops, the continue statement will restart the innermost loop.
Example
Let's look at an example that shows how to use a continue statement in JavaScript.
How to use the Continue Statement with the While Loop
You can also use the continue statement to restart a new iteration of the while loop.
For example:
var counter = 0; while (counter < 5) { counter++; if (counter == 3) { continue; } console.log(counter + ' - Inside while loop on TechOnTheNet.com'); } console.log(counter + ' - Done while loop on TechOnTheNet.com');In this example, the continue statement is used to restart a new iteration of the while loop and skip the remainder of the loop body.
This example will output the following to the web browser console log:
1 - Inside while loop on TechOnTheNet.com 2 - Inside while loop on TechOnTheNet.com 4 - Inside while loop on TechOnTheNet.com 5 - Inside while loop on TechOnTheNet.com 5 - Done while loop on TechOnTheNet.comAs you can see, an entry is not written to the web browser console log when the counter is equal to 3. The continue statement has restarted the loop before the following command can be run (but only when the counter is equal to 3):
console.log(counter + ' - Inside while loop on TechOnTheNet.com');TIP: Notice that in the above example, we have incremented the counter variable at the top of the while loop with the following command:
counter++;We have done this so as to avoid creating an infinite loop in our logic. If our counter had been incremented at the end of the loop, then once the counter is equal to 3, it would get "stuck" at a value of 3 and the while loop would never terminate.
How to use the Continue Statement with the For Loop
You can also use the continue statement to restart a new iteration of the for loop. Let's rewrite our example with the for loop.
For example:
for (var counter = 1; counter < 5; counter++) { if (counter == 3) { continue; } console.log(counter + ' - Inside for loop on TechOnTheNet.com'); } console.log(counter + ' - Done for loop on TechOnTheNet.com');In this example, the continue statement is used to restart a new iteration of the for loop and skip the remainder of the loop body.
This example will output the following to the web browser console log:
1 - Inside for loop on TechOnTheNet.com 2 - Inside for loop on TechOnTheNet.com 4 - Inside for loop on TechOnTheNet.com 5 - Done for loop on TechOnTheNet.comIn this example, an entry is not written to the web browser console log when the counter is equal to 3. The continue statement has restarted the loop before the following command can be run (but only when the counter is equal to 3):
console.log(counter + ' - Inside for loop on TechOnTheNet.com');
NEXT: anchor
Share on: Databases
- SQL
- Oracle / PLSQL
- SQL Server
- MySQL
- MariaDB
- PostgreSQL
- SQLite
MS Office
- Excel
- Access
- Word
Web Development
- HTML
- CSS
- JavaScript
- Color Picker
Programming
- C Language
More
- ASCII
- Unicode
- Linux
- UNIX
- Techie Humor

JS Basics
- Comments
- Console Log
- Literals
- Operators
- Reserved Words
- Variables

JS Loops/Conditionals
- break
- continue
- do-while loop
- for loop
- for-in loop
- if-else
- switch
- while loop

JS String Methods
- anchor
- big
- blink
- bold
- charAt
- charCodeAt
- codePointAt
- concat
- endsWith
- fixed
- fontcolor
- fontsize
- fromCharCode
- fromCodePoint
- includes
- indexOf
- italics
- lastIndexOf
- length
- link
- localeCompare
- match
- normalize
- padEnd
- padStart
- repeat
- replace
- search
- slice
- small
- split
- startsWith
- strike
- sub
- substr
- substring
- sup
- toLocaleLowerCase
- toLocaleUpperCase
- toLowerCase
- toString
- toUpperCase
- trim
- trimEnd
- trimStart
- valueOf

JS Number Methods
- EPSILON
- isFinite
- isInteger
- isNaN
- isSafeInteger
- MAX_SAFE_INTEGER
- MAX_VALUE
- MIN_SAFE_INTEGER
- MIN_VALUE
- NaN
- NEGATIVE_INFINITY
- parseFloat
- parseInt
- POSITIVE_INFINITY
- toExponential
- toFixed
- toLocaleString
- toPrecision
- toString
- valueOf

JS Math Functions
- abs
- acos
- acosh
- asin
- asinh
- atan
- atan2
- atanh
- cbrt
- ceil
- clz32
- cos
- cosh
- E
- exp
- expm1
- floor
- fround
- hypot
- imul
- LN10
- LN2
- log
- log10
- LOG10E
- log1p
- log2
- LOG2E
- max
- min
- PI
- pow
- random
- round
- sign
- sin
- sinh
- sqrt
- SQRT1_2
- SQRT2
- tan
- tanh
- trunc

JS Array Methods
- concat
- copyWithin
- entries
- every
- fill
- filter
- find
- findIndex
Home | About Us | Contact Us | Testimonials | Donate
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.
Copyright © 2003-2026 TechOnTheNet.com. All rights reserved.
Tag » How To Restart Loop C++
-
How Can I Restart While Loop After Certain Condition (if) Satisfied In C++?
-
How To Restart Loop Again? - C++ Forum
-
How Can I Restart A Loop: C++ - For Beginners
-
C++ Loop Help To Restart To Beginning Of Program - C Board
-
Restarting A While Loop Without Finishing The Remaining Code.
-
How To Restart A Loop In Python? - Finxter
-
How To Use A While Loop To Restart A Game - Quora
-
How To Restart Void Loop If Condition Is Met - Arduino Stack Exchange
-
C++ How To Restart The Loop If User Just Presses Enter Or If The Input Is ...
-
How Do I Restart A Function From Within Itself? - Codecademy
-
How To Restart A For Loop In Go - Freshman.tech
-
Exit(0); Restart Loop - Programming Questions - Arduino Forum
-
How Can I Restart While Loop After Certain Condition (if) Satisfied In C++?
-
Going To Start Of A Loop - Scripting Support - DevForum | Roblox