Spidermonkey – Execute javascript from console

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.

apt-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.

$ /usr/bin/smjs
js> function test() {
print('test');
}
js> test
function test() {
    print("test");
}
js> test();
test
js> function test() {
print('test');
test2(123);
}
js> function test2(param) {
print ('test2: ' + param);
}
js> test();
test
test2: 123
js>

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.

document.write('test'); // In browser
print('test'); //spidermonkey

You can also make smjs to execute the content of a file.

$ /usr/bin/smjs test-local.js
Rand: 407
Old: 600
New: 800
Result: old

Leave a Reply