How do you run JavaScript script through the Termi

2019-01-09 22:33发布

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?

13条回答
甜甜的少女心
2楼-- · 2019-01-09 22:52

If your using windows.

  1. Set up node.js in your system and open up node.js terminal
  2. Open the node.js command prompt, navigate to your directory, where you have saved your js file.
  3. and execute your js file by giving a command node yourfilename.js

If you want to execute your javascript code and see the result in the next line, use the codecademy website.

查看更多
Evening l夕情丶
3楼-- · 2019-01-09 22:52

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

查看更多
走好不送
4楼-- · 2019-01-09 22:57

If you have a Mac you can get jsc a javascript console in OS X (Terminal) by typing

/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc

in Terminal.app.

You could also run one of your .js script by adding its name as an argument for jsc, like this:

jsc your_awesome_script_name.js

Notice: I use console.log() during development but jsc needs the debug() 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 and wscript 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").

查看更多
神经病院院长
5楼-- · 2019-01-09 22:58

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.

查看更多
乱世女痞
6楼-- · 2019-01-09 22:59

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.

查看更多
爷的心禁止访问
7楼-- · 2019-01-09 23:04

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.

查看更多
登录 后发表回答