In the Drunk and Retired Podcast, episode 59 I spoke about learning your way around javascript the language independently from the browser, and how you can use the command line tools that come with the various javascript engines to interactively explore the javascript runtime.
When most folks think about javascript, they think about scripts that they embed into their web pages, but the truth is that it is a general-purpose programming language that has absolutely nothing to do with HTML. In fact, the javascript runtime is so orthogonal to other web browser functionality, that mozilla offers the javascript interpreter that it uses in Firefox and friends as a completely separate download. It's available as both .deb or .rpm package, and just to show it: Here's the wonderful hello world program, as entered into the shell.
cowboyd@subzero:~$ js
js> alert('hello world')
1: ReferenceError: alert is not defined
js>
OK, so I boobie-trapped that example in an attempt to beat the point I've been making to death. It's an error because alert() isn't actually part of javascript. In the context with which we're familiar(DHTML), it's a function that's defined by the browser. Of course, it just so happens that every browser implements alert() to behave in almost exactly the same way, but the function itself has nothing to do with the javascript core. Implementing our own version of alert is simple enough though.
js> var alert = function(message) { print(message)}
js> alert('hello world')
hello world
js>
Personally, I love the command line because it let's you dig your fingers deep into the computer's brain and, by pushing its buttons directly, see what's going to work and what isn't. Every time I have a question about how the javascript interpreter is going to behave, I don't look up the spec, or write something into my programs that I'm not sure how it will work. Instead, I fire up my trusty interpreter to discover empirically how the system works. Need to know if a RegExp is going to match? Don't guess. Ask the interpreter.
js> "foo".match(/bar/)
null
js> "foo".match(/oo/)
oo
js> "foo".match(/oo$/)
oo
js>
Wonder what the built-in "constructor" property of an object is? The runtime can tell you. It's his business after all.
js> function A() {}
js> var a = new A()
js> var o = new Object()
js> a.constructor
function A() {
}
js> o.constructor
function Object() {
[native code]
}
js> o.constructor == Object
true
js> a.constructor == A
true
js>
Sure, you could write an in-browser script to do all this and throw output at yourself in the form of alerts, but the beauty of the javascript command line is that you can collapse the whole edit-save-reload-alert scripting cycle into a single step; type in the next line and see what happens. It's that super-tight feedback which let's you learn that much faster.

Delicious
Digg
Reddit
Magnoliacom
Google
Yuh!
Good job documenting it ;)
u shud tll us how 2 install
u shud tll us how 2 install on osx
Debian users: apt-get
Debian users: apt-get install spidermonkey-bin
But most things I want to
But most things I want to try out rely on browser features. Like the DOM. I wish the commandline would help with those somehow.
See Charles? Cote has been
See Charles? Cote has been telling you to blog forever, and now your page is rocking at the top of reddit... Congrats on a cool article. Since 2000 I've been trying to get people to believe that js is a cool language. I remember the shock and horror on people's faces when I would tell them I used js as a _server-side_ language. Hehe.
the Firebug console is great
the Firebug console is great for this ... no need to mock alert and friends
Script I use for same effect
Script I use for same effect on Windows: http://swsci.blogspot.com/2007/07/javascript-from-command-line.html.
This installs the executable
sudo port install spidermonkey
@kartik: as others have
@kartik: as others have pointed out, there are great tools to do this from within the browser if all you're interested in is the exploring the Dom.
The firebug console is fantastic, but it only works in Firefox. For using a javascript shell in both Firefox and Explorer, try the excellent shell from squarefree: http://www.squarefree.com/shell/shell.html
Post new comment