For instance, if you were to run a Python script you would type python filename.py or if you wanted to run a C program make filename then ./ filename. How do you do this with .js files?
If you have Rhino (or alternative) installed and on your path, then running JS can indeed be as simple as
> rhino filename.js
location.href, and create output by appending DOM objects rather than calling
If you've got hold of a script which was written for a web page, you may need to wrap or modify it somewhat to allow it to accept arguments from stdin and write to stdout. (I believe Rhino has a mode to emulate standard browser global vars which helps a lot, though I can't find the docs for this now.)
Another answer would be the NodeJS!
Using terminal you will be able to start it using
$ node > 2 + 4 6 >
Note: If you want to exit just type
If you have a Mac you can get
You could also run one of your .js script by adding its name as an argument for jsc, like this:
Notice: I use
console.log() during development but jsc needs the
debug() function instead.
On Windows as other people said you can rely on
wscript directly built on the OS.
Use node.js for that, here is example how to install
node by using
brew on mac:
brew update && install node
Then run your program by typing
node filename.js, and you can use
console.log() for output.
If you're using MacBook.
node.jsin your system and open up the terminal
example, if filename is
They also have a Python and Ruby IDE.
If you are on a Windows PC, you can use WScript.exe or CScript.exe
Just keep in mind that you are not in a browser environment, so stuff like
document.write or anything that relies on the
window object will not work, like
window.alert. Instead, you can call
WScript.Echo to output stuff to the prompt.
I tried researching that too but instead ended up using jsconsole.com by Remy Sharp (he also created jsbin.com). I'm running on Ubuntu 12.10 so I had to create a special icon but if you're on Windows and use Chrome simply go to Tools>Create Application Shortcuts (note this doesn't work very well, or at all in my case, on Ubuntu). This site works very like the Mac
jsc console: actually it has some cool features too (like loading libraries/code from a URL) that I guess
jsc does not.
Hope this helps.
This is a "roundabout" solution but you could use ipython
Start ipython notebook from terminal:
$ ipython notebook
All the answers above are great, I see one thing missing and could be considered for running
JDK comes up with two nice tools, could be utilized for executing
Here are command goes like. Make sure to navigate to
Its comes up with another
commmand tool that goes like this-
I hope this may be helpful to others.
You can also use
phantomjs Download phantomjs depending on the system (my case is Max OSX) from phantomjs.org
> phantomjs filename.js
$ /Applications/Firefox.app/Contents/MacOS/firefox -headless index.html *** You are running in headless mode.
All you have to do to run a js file via bash is type:
$ node filename.js
This is similar to in python, when you do:
$ python filename.py
©2020 All rights reserved.