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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
On Ubuntu, install libjavascriptcoregtk-3.0-bin and use
/usr/bin/jsc
(manpage).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
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 callingprint
.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.)
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 guessjsc
does not.Hope this helps.
This is a "roundabout" solution but you could use ipython
Start ipython notebook from terminal:
$ ipython notebook
It will open in a browser where you can run the javascript
All the answers above are great, I see one thing missing and could be considered for running
javascripts(*.js)
files, the unrelated brother ofjavascript
theJava
.JDK
comes up with two nice tools, could be utilized for executingjavascripts
. Here are command goes like. Make sure to navigate toJDK\bin
.Its comes up with another
commmand
tool that goes like this-I hope this may be helpful to others.
Another answer would be the NodeJS!
Using terminal you will be able to start it using
node
command.You can also run a JavaScript file like this:
« Install it NOW »