Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect for...of Loop Support in JavaScript

Is this possible? Example:

var parts = [1,2,3,4,5];
for (part of parts) {
    console.debug(part);
}

I want to detect if doing this is possible.

like image 483
Gabriel Nahmias Avatar asked Aug 17 '13 01:08

Gabriel Nahmias


1 Answers

You can always try-catch such stuff. But you need eval as well, as some javascript engines will bail with a SyntaxError early.

try {
  eval("for (var i of []);");
  console.log("yep");
} catch(ex) {
  console.log("nope");
}

Tested in Firefox ("yep") and Chrome ("nope").

like image 183
nmaier Avatar answered Sep 18 '22 21:09

nmaier