Additional to @svlin advice about how use the State API to reference Story variables within JavaScript code.
The State.temporary property can be used to reference Temporary variables.
SugarCube’s engines uses a Private Scoped Context to execute JavaScript.
Things defined in one Private Scoped context are not available in another such scoped context.
The let keyword/statement is used to create JavaScript variables, not the Story or Temporary variables supported by SugarCube.
The last three points are the main reason why the JavaScript variable you created using the let keyword in the context of the <<script>> macro call was not available in the context of the <<print>> macro call, thus the “undefined” error.