Does `for (;;)` (two semicolons) in JavaScript mean infinity?

Does `for (;;)` (two semicolons) in JavaScript mean infinity?

Problem Description:

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.

Solution – 1

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).

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject