Is eval in Javascript safe if it doesn't use variable code?

Just testing to support const in browsers, so I need to catch syntax errors that you can only do with eval.

var const_support = true;
try{
    eval("const test = 'test';");
}
catch(e){
    if (e instanceof SyntaxError) {
        const_support = false;
    }
    else{
        throw ("Something crazy happened.");
    }
}

Greetings.

Edit: Sorry for the duplicate. In any case, like an epascarello solution. Avoids eval.

+3
source share

All Articles