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").