-3

It's in JavaScript not C language, so I think it's not duplicate

I was testing some code and I found this one :-

for (;;) {
  console.log("test");
}

And the iterations kept going forever

I was wondering what does this ;; mean? And what is its use case?

PS :- don't run it as it will freeze for infinite iteration.

1 Answers1

2

The reason that for(;;) loops forever is because for has three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:

for(i = 0; i < 10; i++)

If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though true were there in its place. So for(;;) is the same as for(;true;), which is the same as while(true).

Usitha Indeewara
  • 870
  • 3
  • 10
  • 21
  • Please delete your answer as it's duplicate, and I can't delete the duplicate question –  Aug 06 '23 at 17:59