SpiderMonkey is the code-name for the Mozilla’s C implementation of JavaScript. This is useful to test part of our JavaScript from the console or in scripts.
In Debian we have a package called spidermonkey-bin.
1apt-get install spidermonkey-bin
After installing you will have a program called smjs.
If you start the program without parameters you will get a Javascript shell, in which you can write and test javascript.
1$ /usr/bin/smjs
2js> function test() {
3print('test');
4}
5js> test
6function test() {
7 print("test");
8}
9js> test();
10test
11js> function test() {
12print('test');
13test2(123);
14}
15js> function test2(param) {
16print ('test2: ' + param);
17}
18js> test();
19test
20test2: 123
21js>
To exit the shell, just press “Ctrl+D“.
It’s important to note that in spidermonkey you doesn’t have the “document” Object. If you want to print out text, you cant use document.write, you should use print.
1document.write('test'); // In browser
2print('test'); //spidermonkey
You can also make smjs to execute the content of a file.
1$ /usr/bin/smjs test-local.js
2Rand: 407
3Old: 600
4New: 800
5Result: old