How does the semicolon work at the beginning of the "for"?

I just came across this code on the Mozilla website, and although it seems broken to me, most likely I am not familiar with its use:

for (; k < len; k++)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }

How does the semicolon at the beginning of the loop work?

Full code here.

+5
source share
2 answers

The first part is the initial expression that is used to initialize the variables (see forconstruct ):

 for ([initial-expression]; [condition]; [final-expression])
    statement

, . - , . , k for:

var k = n >= 0
      ? n
      : Math.max(len - Math.abs(n), 0);

for (; k < len; k++)
{
  if (k in t && t[k] === searchElement)
    return k;
}

, :

for (var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); k < len; k++)
{
  if (k in t && t[k] === searchElement)
    return k;
}
+12

, k - - ;

, , :

for (;;) {
  //infinite loop
}
+1

All Articles