The easiest way to do this on Windows is to hold down the SHIFT key and then right click, then choose Open command window here. From the command window, type the command node and your command prompt will be transformed in to an interactive JavaScript REPL.
Expanding upon the solution to use Node.js…
Here are some examples and screenshots from a page on Command Line JavaScript.
If you enter node
on the command line with no arguments, you'll be in the Read-Eval-Print-Loop, or REPL for short, otherwise known as a shell. Here you can interactively enter JavaScript expressions and have them immediately evaluated.
Create a file with the following content:
console.log('Hello, world');
From the command line, use node
to evaluate the file:
If your tests need access to a DOM there is always PhantomJS - a headless (Webkit) browser.
I am not saying that it is the best solution but it is one of the available options. I just want to spread awareness and one reason this is how Java runs javascript because it has an embedded JavaScript runtime already for a long time. First there was Rhino, and now, Java SE 8 shipped with a new engine called Nashorn, which is based on JSR 292 and invokedynamic. It provides better compliance with the ECMA normalized JavaScript specification and better runtime performance through invokedynamic-bound call sites. It can be used to run JavaScript programs from the command line. To do so, builds of Oracle’s JDK or OpenJDK include a command-line tool called jjs. It can be found in the bin/ folder of a JDK installation along with the well-known java, javac, or jar tools.
The jjs tool accepts a list of JavaScript source code files as arguments. Consider the following hello.js file:
var hello = function() {
print("Hello Nashorn!");
};
hello();
Evaluating it is as simple as this:
$ jjs hello.js
Hello Nashorn!
$
For more detail you can refer official documentation http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html
Yeah, It can be possible with node
.
Just create a js file, write some code, save that and go to the directory where you have saved your file, then enter node <your file_name>
.
You are done.
Note: You must have node
installed on your system.
You can do this using node.js. You can run each file separately, but of course I'm assuming there's no dependency between files.
This windows command line javascript discusses using Windows Scripting Host if you're on Windows and don't want to install Node. But Node is probably the better bet for standardized js (it uses the v8 Javascript engine).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With