DEV Community

Cover image for Talking about "for" and "while" loops in JavaScript
Saqib Suleman
Saqib Suleman

Posted on

1 2

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

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

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay