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
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
You would need a JavaScript engine (such as Mozilla's Rhino) in order to evaluate the script - exactly as you do for Python, though the latter ships with the standard distribution.
If you have Rhino (or alternative) installed and on your path, then running JS can indeed be as simple as
> rhino filename.js
It's worth noting though that while JavaScript is simply a language in its own right, a lot of particular scripts assume that they'll be executing in a browser-like environment - and so try to access global variables such as location.href
, and create output by appending DOM objects rather than calling print
.
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.)
Alternatively, if you're just looking to play around with Javascript a nice in browser option is Codecademy's Javascript Lab.
They also have a Python and Ruby IDE.
Technically, Node.js isn't proper JavaScript as we know it, since there isn't a Document Object Model (DOM). For instance, JavaScript scripts that run in the browser will not work. At all. The solution would be to run JavaScript with a headless browser. Fortunately there is a project still active: Mozilla Firefox has a headless mode.
https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode
$ /Applications/Firefox.app/Contents/MacOS/firefox -headless index.html
*** You are running in headless mode.