Search code examples
javascriptenginejavascript-engine

ReferenceError in ScriptEngine eval


I'm using the JavaScript engine in JDK 7. Is there a way to evaluate an expression like a||b to false if a and b are undefined, instead of throwing the exception ReferenceError: "a" is not defined.?


Solution

  • I don't think this is possible.

    I took the following approach to achieve what I wanted. I was evaluating the expression and if that expression was failing, I was adding the undefined variable to the context and try to reevaluate again.