-1

So, Iwas reading through some Javascript Question answers on CodeGolf to try and improve my understanding of Javascript a lttle bit, & noticed this answer in the following question Generate Green Spraypaint Patterns Within the Answer I spotted a for loop that..[to me] is not the usual Syntax, & no matter how much I stare at it I can't figure out what this does. I can't find any referance to an operator ;; on Mozilla's Javascript Operators page & was wondering if anyone can explain What the double semi-colon does in the code below?

b.onclick = async function() {
  b.disabled = true;
  try {
    await f(c, +s.value, +n.value, +m.value, JSON.parse(d.value));
  } catch (ex) {
    console.error(ex);
  }
  b.disabled = false;
}
async function f(canvas, size, number, moves, deltas) {
  canvas.width = canvas.height = size;
  let context = canvas.getContext('2d', { alpha: false });
  context.fillRect(0, 0, size, size);
  let image = context.getImageData(0, 0, size, size);
  for (let i = 0; i < number; i++) {
    let x = size / 2 | 0, y = x, j = moves;
    for (;;) {
      image.data[(x + y * size) * 4 + 1]++;
      if (!j--) break;
      let valid = deltas.map(([dx, dy]) => [x + dx, y + dy]).filter(([x, y]) => x >= 0 && y >= 0 && x < size && y < size);
      if (!valid.length) break;
      [x, y] = valid[valid.length * Math.random() | 0];
    }
    context.putImageData(image, 0, 0);
    await new Promise(requestAnimationFrame);
  }
}

<div>Size: <input id=s type=number min=1 value=250></div>
<div>Pixels: <input id=n type=number min=0 value=800></div>
<div>Moves: <input id=m type=number min=0 value=7000></div>
<div>Deltas: <input id=d value=[[1,2],[-1,2],[1,-2],[-1,-2],[2,1],[-2,1],[2,-1],[-2,-1]]></div>
<div><input id=b type=button value=Go!></div>
<canvas id=c>

Might be really simple, but just curious to learn. Is it just a placeholder to grab the exact same info from the for loop that its nested with-in?

Just for anyone else's reference, thats beginning or learning Javascript none of the below work.. lol enter image description here

Ryan Stone
  • 345
  • 4
  • 19

1 Answers1

3

for(;;) is a for loop declaration with:

  • no initialization code
  • no end-of-block expression
  • no terminating condition

So, it's equivalent to while(true) - it'll loop forever until a break inside it is encountered.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Why was 8 not chosen for this syntax seeing as 8 typically represents infinity? at least in maths & science, possibly mythology or esoteric stuff? – Ryan Stone Nov 05 '20 at 21:00
  • @RyanStone: 8 is the number eight, not infinity. There is the variable `Infinity` though representing that. How would you use it in this case? – Felix Kling Nov 05 '20 at 21:02
  • `8` means the literal number 8. If it also meant to loop forever under certain syntactical situations, that'd be pretty confusing. `while(true)` is a common thing seen across many languages, in contrast. – CertainPerformance Nov 05 '20 at 21:02
  • 2
    @RyanStone This isn't any special syntax, it's exactly the same as a "normal" for loop with the usual contents left out (i.e. `for(i=0; i<5; i++)` becomes `for( ; ; )` if you leave out `i=0`, `i<5` and `i++`). BTW the symbol for infinity is ∞ not the number 8. – Guy Incognito Nov 05 '20 at 21:06
  • Does `;;` have any use cases outside of a for loop or is that the only place where it would be valid syntax? – Ryan Stone Nov 05 '20 at 21:11
  • @RyanStone Extraneous semicolons are permitted. Eg `const foo = 'bar';;` is allowed, so pretty much anywhere you could encounter the end of a statement, you could have `;;` there. – CertainPerformance Nov 05 '20 at 21:13