Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Firefox complaining about a semicolon in this javascript for loop?

So I have this really basic function with a for loop. It runs fine on modern Chrome and Firefox browsers, but not on a particularly picky Firefox 38 browser. According to the docs this function has been supported since Firefox 13.

function showhide_class(cl) {
  var es = document.getElementsByClassName(cl);
  for(let e of es) {
  e.style.display = (e.style.display == "block") ? "none" : "block";
  }
}

The exact error being reported by Firefox is:

SyntaxError: missing ; after for-loop initializer

So, why is this error being reported and do you know of a work around? Thanks so much.

like image 264
Bryant Avatar asked Aug 26 '16 21:08

Bryant


1 Answers

The let statement is the problem. Use for (var e of es) instead.

According to MDN, the let statement did not get support in Firefox until version 44.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

like image 116
Reid Horton Avatar answered Nov 10 '22 22:11

Reid Horton