Loops in JavaScript are used to perform repeated tasks based on a condition. Conditions typically return true or false when analysed. A loop will continue running until the defined condition returns false, The JavaScript loops are used to iterate the piece of code using for, while, do-while. It makes the code compact. It is mostly used in an array.

For Loop
The for loop statement in JavaScript allows you to create a loop with three optional expressions. The for loop is the simplest type of loop. It consists of the three key components listed below:


first The startup of the loop, when we set our cars to a beginning value. Before the loop starts, the initialization statement is executed.
secondly, The test statement determines whether or not a particular condition is true. If the condition is true, the code inside the loop will be performed; if it is false, the control will exit the loop.
The iteration statement is the last element when you can increase or decrease.


The while loop statement generates a loop that runs a specified statement as long as the test condition is true. Before executing the statement, the condition is checked.

While loop Syntax

The code in the loop in the following example will be executed repeatedly as long as a variable I is less than 10, note that the loop will never end if you fail to increment the variable used in the condition. This will cause your browser to freeze.


The do-while loop is similar to the while loop in that it checks the condition only after it has completed its instructions, and it always runs at least once.


The main difference between a while and a do-while loop is the location where the condition is checked. The while loop checks the condition before running any of the while loop’s statements, whereas the do-while loop tests the condition after the loop’s statements have been performed.




enthusiastic software engineer, open to other people’s opinions always eager to explore

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to get device location without using GPS tracker in React Native?

Highlights from React Live Amsterdam 2019

Vue.js vs ReactJs — Which one Should be chosen to create an app?

Using React with canvas element

About Me — RayRay

Vowels with JavaScript

Black Friday Discount , Web Dev news, VueJS & Frontend Love Conference 2018

What is JSF?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nwaubani Miracle

Nwaubani Miracle

enthusiastic software engineer, open to other people’s opinions always eager to explore

More from Medium

The equality (==) and strict equality (===) operators in JavaScript

Use normalize() to duel with non-English string in javascript

Typescript Basics: Inferences

Multiple node versions on a windows machine [latest version]