Run Javascript in Command Prompt with WSH

When one initially happens upon Node.js, there is something awesome about being able to run javascript in a system console. Running javascript in a browser is cool and all, but executing some javascript in a console under Windows just feels too raw to be true. Windows Script Host can run both VBScript or JScript. Ok. So we don’t actually care what WSH is capable of, we just want to run javascript in command prompt, right? This “hello WSH” type script will evaluate javascript statements and print the output. Sort of feels like running javascript on a Node.js REPL?

  • Clone this gist from https://gist.github.com/5718851.git
  • Execute the following script in command prompt with cscript WSHRepl.js and type in as many js one-liners as suits your fancy.

Behold the Windows Script Host REPL

function print(text) {
    WScript.Echo('> ' + text);
}

var stdin  = WScript.StdIn;
var stdout = WScript.StdOut;
var input;

do {
    var input = stdin.ReadLine();
    try {
        print(eval(input));
    } catch (e) {
        print(e.name + ': ' + e.message);
    }
} while (input != 'exit');

This obviously isn’t very useful other than maybe fiddling with Microsoft’s implementation of ECMAScript. I used the WSH Reference to find the WScript.StdOut and WScript.StdIn objects.