Javascript – eval javascript, check for syntax error


I wanted to know if it is possible to find through javascript if a call to eval() has a syntax error or undefined variable, etc… so lets say I use eval for some arbitrary javascript is there a way to capture the error output of that eval?

Best Solution

You can test to see if an error is indeed a SyntaxError.

try {
} catch (e) {
    if (e instanceof SyntaxError) {