view raw
Sergey  Eliseev Sergey Eliseev - 5 months ago 62
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?


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