For the last month, I’ve been reconditioning one of the flats I own in Bucharest, I felt exhausted and all the construction work reminded me of my early days in Spain where I started to work so that I could sustain myself. Nevertheless, the feeling I had “while” working it was for a continuous loop in Javascript.
This week, I’ll tackle the Loop in Javascript, so the basic idea behind a loop is to automate the repetitive tasks within a program to save time and effort.
To have a better understanding of what I do is to try to explain to my kids what I’ve learned in Javascript when I talk to them about my work so that I’ll develop in them (hopefully) the same passion I have for coding. So, the other day, I was thinking about how to explain this to my 7th-year daughter the Loop. I had the idea of asking on Twitter the DEVCommunity, and I received plenty of answers that helped me understand deeply.
Among all the types that JavaScript Loop has I’ve chosen only the below:
- while
- do-while
- for
While in ES6 we also have the “for-of” Loop, I will stick in this article only to the three mentioned by bullets.
So, what is The Line that Brings Eternity to JavaScript?
While
The while loops through a block of code as long as the condition specified evaluates to true, when the condition fails, the loop stops. The syntax of the while loop is the following:
while(condition) {
// Execute Code
}
I also added an example of while where it defines a loop that will continue to run as long as the variable “i” is less or equal to 7. The variable will increase each time the loop runs up to 7.
Do While
The do-while loop evaluates the condition at the end of each loop iteration. The block of code is executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated is true. I added the syntax below:
do {
// Execute Code
}
while(condition);
As you can see in the image below, the Loop stops at seven.
For Loop
While the “while Loop” goes through a block of code as long as the condition specified evaluates to true, the for loop repeats a block of code as long as a certain condition is met. It is used to execute a block of code for a certain number of times. The syntax for “for Loops” is the following:
for (initialization; condition; increment) {
// Execute Code
}
The following example in the below photo defines a for loop that starts with 0. The loop will continue until the value of the variable i is less than or equal to 7. The variable i will increase by 1 each time the loop runs.
This is all for this week, but before I’ll let you go, I got 2 things that I want you to take a look:
First is a Twitt that became a thread and has so many diverse answers and with it so valuable information regarding Loop which is here if you ask me, the best and most accurate answer is this:

and second, of it, it is the line that will bring eternity into your machine:
// Infinite loops are bad and they are those that bring eternity into your machine:
while (true) {
console.log(‘Bring Eternity!’);
}
Spoiler Alert: do NOT DO this as it will put your browser on a continuous loop and you might have to shut down your computer as it will turn unresponsive.
This was Luc, a frontend developer in his 40’s that decided to change his career in 2019.
Want to know more also about ceiling in JavaScript? Why not read this article?
Until next time, keep crushing it and have fun.