title: Why is Java (Nashnorn library) not evaluationg JS code correctly?
body: <p>I have code that works fine in JavaScript IDE like Webstorm, but when I try to port it to Java, using the Nashorn library (to run JS within a Java program), <strong>I get these errors:  &quot;Name: undefined, Age: undefined&quot;</strong> (with possibly others to follow depending what's wrong here.)</p>
  <p>Below is the full code. This coding approach below seems to work in general, but there are cases like this where I don't get the expected results.</p>
  <p>Would appreciate any help or suggestions.</p>
  <p>Thanks!</p>
  <hr />
  <pre><code>public class JavaScriptEvaluationExample
  {
      public static void main(String[] args) throws ScriptException, NoSuchMethodException
      {
          // Basic JavaScript evaluation
          String json = &quot;{\n&quot; +
                  &quot;    \&quot;name\&quot;: \&quot;John\&quot;,\n&quot; +
                  &quot;    \&quot;age\&quot;: 30,\n&quot; +
                  &quot;    \&quot;cars\&quot;: [\n&quot; +
                  &quot;        {\n&quot; +
                  &quot;            \&quot;name\&quot;: \&quot;Ford\&quot;,\n&quot; +
                  &quot;            \&quot;models\&quot;: [\n&quot; +
                  &quot;                \&quot;Fiesta\&quot;,\n&quot; +
                  &quot;                \&quot;Focus\&quot;,\n&quot; +
                  &quot;                \&quot;Mustang\&quot;\n&quot; +
                  &quot;            ]\n&quot; +
                  &quot;        },\n&quot; +
                  &quot;        {\n&quot; +
                  &quot;            \&quot;name\&quot;: \&quot;BMW\&quot;,\n&quot; +
                  &quot;            \&quot;models\&quot;: [\n&quot; +
                  &quot;                \&quot;320\&quot;,\n&quot; +
                  &quot;                \&quot;X3\&quot;,\n&quot; +
                  &quot;                \&quot;X5\&quot;\n&quot; +
                  &quot;            ]\n&quot; +
                  &quot;        },\n&quot; +
                  &quot;        {\n&quot; +
                  &quot;            \&quot;name\&quot;: \&quot;Fiat\&quot;,\n&quot; +
                  &quot;            \&quot;models\&quot;: [\n&quot; +
                  &quot;                \&quot;500\&quot;,\n&quot; +
                  &quot;                \&quot;Panda\&quot;\n&quot; +
                  &quot;            ]\n&quot; +
                  &quot;        }\n&quot; +
                  &quot;    ]\n&quot; +
                  &quot;}&quot;;
          
          String jsScript = &quot;function parseJSONFields(param)\n&quot; +
                  &quot;{\n&quot; +
                  &quot;    var result = \&quot;\&quot;;\n&quot; +
                  &quot;    var carName =\&quot;\&quot; ;\n&quot; +
                  &quot;    var models = \&quot;\&quot;;\n&quot; +
                  &quot;    var name = param.name + \&quot;,\&quot;;\n&quot; +
                  &quot;    var age = param.age;\n&quot; +
                  &quot;    result = \&quot;Name: \&quot; + name + \&quot; Age: \&quot; + age + \&quot;\\n\&quot;;\n&quot; +
                  &quot;    for (item in param.cars)\n&quot; +
                  &quot;    {\n&quot; +
                  &quot;        result  += \&quot; \&quot;+ \&quot; &gt;&gt; \&quot; + param.cars[item].name +\&quot;: \&quot;\n&quot; +
                  &quot;        result+= \&quot; \&quot;  + param.cars[item].models\n&quot; +
                  &quot;        result += \&quot;\\n\&quot;;\n&quot; +
                  &quot;    }\n&quot; +
                  &quot;    return result;\n&quot; +
                  &quot;}&quot;;
  
          System.out.println(runJS(&quot;parseJSONFields&quot;, json, jsScript));
      }
  
      // ------------------------------------------------------------------------------------------------
      private static Object runJS(String functionName, String param, String script) throws ScriptException, NoSuchMethodException
     // ------------------------------------------------------------------------------------------------
      {
          Object resultString = &quot;&quot;;
          String errorResultString = &quot;&quot;;
  
          ScriptEngine js = null;
          try
          {
              if (param.length() &gt; 0)
              {
                  js = new ScriptEngineManager().getEngineByName(&quot;javascript&quot;);
                  js.eval(script);
                  Invocable inv = (Invocable) js;
              }
          }
          catch (Exception e)
          {
              errorResultString = e.getMessage() + &quot; ---&gt; &quot; + e.getMessage();
          }
  
          return errorResultString.length() &gt; 0 ? errorResultString
                  : ((Invocable) js).invokeFunction(functionName, param);
      }
  }
  </code></pre>