Sergey  Eliseev Sergey Eliseev - 9 days ago 6
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

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