Sergey  Eliseev Sergey Eliseev - 1 year ago 127
Java Question

Differ null and undefined values in Nashorn

I'm running this code

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("var out;");
engine.eval("var out1 = null;");
Object m = engine.get("out");
Object m1 = engine.get("out1");

And getting m == null and m1 == null.

How to determine if value is undefined or null?

Answer Source

Java doesn't have a concept of "undefined", so understanding the distinction will require expressing it in the script's language. I suggest using this expression:

Boolean isUndefined = engine.eval("out === undefined");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download