JDK-8057691 : Nashorn: let & const declarations are not shared between scripts
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u40,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2014-09-05
  • Updated: 2015-06-04
  • Resolved: 2014-11-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40 b17Fixed 9Fixed
Description
I tried with jjs
$ jjs --language=es6                                                                                                                                                      
jjs> var x = 2;
jjs> let a = 1;
jjs> x
2
jjs> a
<shell>:1 ReferenceError: "a" is not defined
jjs> 

Comments
I noticed this as well. The thing is that in the shell we handle each line of input as separate script, and the lexically defined variables are only defined in that script. In order to make them visible throughout a shell session we'd have to preserve that per-script scope somehow. I noticed that this works in V8 shell so they must do something to "connect" the input lines like reusing the per-script scope. I also noticed that the same problem occurs for normal vars in -strict mode: $ jjs -strict jjs> var x = 123 jjs> x <shell>:1 ReferenceError: "x" is not defined
05-09-2014