DEV Community

loading...
Cover image for Talking about "for" and "while" loops in JavaScript

Talking about "for" and "while" loops in JavaScript

Saqib Suleman
Self-taught Web developer
・1 min read

Looping in JavaScript is a time-efficient method to actually run a code more than once according to the requirement. Loops are very helpful in most cases and save a great deal of time and effort. There is various type of loops that are used in different situations as explained below:

For Loops
For loops are used where some code is to be repeated a number of time that can be predetermined by giving it specific instructions such as

for (let i=1; i<=6; i++){
    console.log("Da ba dee da ba daa");
}
Enter fullscreen mode Exit fullscreen mode

While Loops
While loops are used where it cannot be pre-determined how many times the code will actually run. In while loops, the code keeps repeating "while" the given condition is not satisfied as shown below

while (i < 10) {
  text += "The number is " + i;
  i++;
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)