Can anyone explain me why the following Java code which executes the JavaScript return "Smith" here?
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
String jsSnippet = "var fullName = 'Joe Smith'; fullName.substring(fullName.indexOf(' ') + 1);";
System.out.println(engine.eval(jsSnippet));
Similar JavaScript code (jsFiddle) which gives the same output as above Java code is
var jsSnippet = "var fullName = 'Joe Smith'; fullName.substring(fullName.indexOf(' ') + 1);";
document.write(eval(jsSnippet));
PS: I know eval is evil, but this is one of the circumstances I have to use JavaScript eval.
The eval function evaluates the last expression passed into it, and returns its value. See the relevant section on eval at the Mozilla Developer Network.