Javascript from the command line

Javascript has come a long ways. The Mozilla project has released a couple of tools for javascript writers. I recently added the capability to my Mac of being able to write javascript then run and debug it from the command line, just like I would a perl script. Rhino is a java tool that will run your javascript code from the command line. The best part about it is that the debugger is great. It shows you were your code is wrong and gives a guess as to what is wrong about it.

I found this blog entry to give me the steps to enable rhino. I created an alias named js in my .bash_profile. Now I can write a javascript and to call and run it I just type in js /path/to/script.js and it will run and debug. What I usually do is write a function, then pass it some parameters and use the print() function to print to the terminal my results. This is similar to writing alert() or document.write in a web page to evaluate a function.

To get the most out of Javascript read the Mozilla.org javascript documents.

Comments are closed.