NodeJS REPL

mini-blog –> short, quick blog entry to share something cool and useful.

New to Node JS and still working via file processing?  This can get really tedious if you’re like me and would like to experiment on the fly.  Well, there’s a nice little utility in Node that implements a REPL (read-eval-print loop)!   If you’ve ever accidentally typed “node”, at your command, without specifying your script file to run, then you’ve taken yourself–inadvertently, albeit–to Node’s REPL interface.  The default prompt is simply a greater-than sign, “>”.

Here, you can type in Javascript expressions and blocks of code, just like you would in a script file.  The result is whatever the expression returns.  Type, “x = 2″ and you’ll get back “2″.  Type, “var x = 2″, and you’ll get back undefined, because a declaration doesn’t evaluate to anything when executed.   But, now that it’s buffered, you can type “x” and get back the value, i.e. “2″.

Here’s a little hint, type “.help” to get a list of commands for the REPL interface as implemented by default.

Feel free to go through the Node.js docs to get more details.