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?
If your using windows.
node.js
in your system and open upnode.js
terminalnode.js
command prompt, navigate to your directory, where you have saved your js file.node yourfilename.js
If you want to execute your javascript code and see the result in the next line, use the
codecademy
website.You can also use
phantomjs
Download phantomjs depending on the system (my case is Max OSX) from phantomjs.org .You should put the path to phantomjs installation folder on the top of your javascript file. eg.#!./bin/phantomjs
Save your code. Go to the terminal where your javascript is saved and you can run using> phantomjs filename.js
If you have a Mac you can get
jsc
a javascript console in OS X (Terminal) by typingin Terminal.app.
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 thedebug()
function instead.On Ubuntu you have some nice ECMAScript shells at your disposal. Between them it's worth to mention SpiderMonkey. You can add It by
sudo apt-get install spidermonkey
On Windows as other people said you can rely on
cscript
andwscript
directly built on the OS.I would add also another :) way of thinking to the problem, if you have time and like to learn new things i'd like to mention coffee-script that has its own compiler/console and gives you super-correct Javascript out. You can try it also on your browser (link "try coffeescript").
It is crude, but you can open up the Javascript console in Chrome (Ctrl+Shift+J) and paste the text contents of the *.js file and hit Enter.
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.
Use node.js for that, here is example how to install
node
by usingbrew
on mac:Then run your program by typing
node filename.js
, and you can useconsole.log()
for output.