Search notes:

The SpiderMonkey JavaScript shell

A JavaScript shell is quite useful to quickly test or debug snippets of JavaScript code without a browser.
The shell is started with the js or js24 command.

Running a script

In order to run a sequence of JavaScript statements (a script), the shell can be invoked with the name of the script:
$ js24 script.js
The content of script.js might just be something like this:
var i = 39;
var j =  3;

print(i+j);
Github repository about-SpiderMonkey-shell, path: /script.js
Apparently, the -f option can be used to explicitly state the name of the script:
$ js24 -f script.js

Evaluating a string

A string can be evaluated as JavaScript with the -e option:
$ js24 -e 'print (6*7)'

Executable scripts

A script can be chmod'ed to contain the executable bit so that a script can be executed directly from the shell:
$ ./executable-script.js
executable-script.js might look like this:
#!/usr/bin/env js24

print ('This script can be started on');
print ('the shell with:');
print ('');
print ('   ./executable-script.js');
Github repository about-SpiderMonkey-shell, path: /executable-script.js
Note, Mozilla's Introduction to the JavaScript shell proposes to add a -P in the first line. I found that to be wrong, at least in my environment.

Downloading the executable

A zipped executable can be downloaded from the nightly builds directory (filename jsshell-….zip)

Windows / PowerShell

On Windows, it is possible to download the executable with PowerShell:
(new-object Net.WebClient).DownloadFile('https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-win64.zip', "$env:temp\jsshell.zip")

$destDir = "$home/bin/jsshell"
$null    =  mkdir $destDir

c:\windows\system32\tar.exe xf  $env:temp\jsshell.zip -C $destDir

& $destDir\js.exe -v

See also

SpiderMonkey

Index