I recently came to this thread, and had a quick question on the syntax used in the first answer. @ggorlen used this syntax/notation in the for loop that I've never seen before and couldn't find any answers online:
for (;;) {
try {
await page.waitForFunction(
`${thumbs.length} !==
document.querySelectorAll("#video-title").length`,
{timeout: 10000}
);
}
catch (err) {
break;
}
thumbs = await page.$$eval("#video-title", els => {
els.at(-1).scrollIntoView();
return els.map(e => e.getAttribute("title"));
});
}
What does the for(;;) {...} do?
Thanks!
I just have a question on the syntax used, and couldn't find an answer.
is the equivalent of
a normal "for" loop contains 3 parts separated by semi-colons like
for(initialization; condition; afterthought)The initialization runs before the looping starts.
The condition is checked at the beginning of each iteration, and if it evaluates to true, then the code in the loop body executes.
The afterthought is executed at the end of each iteration of the loop.
It is allowed to ommit these parts of the for expression e.g.
for(;;)When omitted it essentially means that it will loop forever since the condition is not present or until the code reaches areturnorbreak.You can read more about the
forfunction checkout this MDN page.