Setup and Using Javascript

Twine Version:
[also choose a Story Format tag above]
Sugarcube2
I cannot get javascript to work, even with simplest example. It always returns the following error:
An error has occurred. You may be able to continue, but some parts may not work properly.
Error [tw-user-script-0]: expected expression, got ‘<’.
This happens no matter what javascript code I use.

Here is my simplest passage example, that produces the error:

<script> console.log("Hello"); </script>

Hi There!

SugarCube has a special macro for that: <<script>>.

Beyond what @manonamora said about the <<script>> macro—note the number of angle brackets—for scripting within passages, the content of your project’s Story JavaScript section is already treated as JavaScript, you do not use HTML within it.

For example, in the Story JavaScript section just do:

console.log("Hello, world!");

And within a passage:

<<script>>
console.log("Hello, world!");
<</script>>

NOTE: Story Formats—e.g., SugarCube—are single-page (web) applications (SPA). They are not regular web pages, so JavaScript code may, or may not, behave as you’d expect from a normal web page. In particular to SugarCube, each JavaScript instance that runs within SugarCube itself is contained within its own scoped instance, which has some consequences depending on what you’re doing.