News for hackers. Stuff that matters.

Yes, I know how to write programs. How do I *run* them?

Over here I set out the basic question:

http://news.ycombinator.com/item?id=2713391

It seems clear that I've asked the wrong question. Or at least, not asked the question I thought I asked. But certainly not asked the question I wanted to ask.

I know how to program. I have written JavaScript code by following the example syntax and adapting the given examples. Fine.

How do I run the bloody thing?

For Python, I do this:

* Open an editor.

* Write the code

* Save the code as code.py

* Exit the editor

* Type "Python code.py"

* Job done.

I can chmod +x the file and put it on my path and use it as an executable.

I'm looking for the equivalent(s) for JavaScript.

I think that's the question I intended to ask. This information seems sorely lacking in all the "tutorials" I've come across so far.

( 17 comments | 7 points | on HN )
Re:Yes, I know how to write programs. How do I *run* them? by chc on Thursday June 30, 2011 @01:12PM (#2714849)
I don't want this to seem like a dismissal, but I'd strongly suggest taking this kind of thing to Stack Overflow or similar. Stack Overflow is optimized for "How do I do this programming task?" kinds of questions, whereas Hacker News is a badly overloaded news/link aggregator. Your question won't get pushed off the Stack Overflow JavaScript category by a story about how somebody built his blog of cats in wedding dresses up to $300/mo in revenue.
Re:Yes, I know how to write programs. How do I *run* them? by mikeocool on Thursday June 30, 2011 @10:36AM (#2714190)
Based on your original question, it sounds like you want to run js in the browser and not do server side js?

If that's the case create this html file: <html> <head> <script type="text/javascript" src="whatever.js"></script> </head>

<body></body> </html>

And place it in the document root of the web server running on your machine, with your js code in a file called whatever.js alongside it. Then navigate to localhost in your browser, the js will run when the page loads.

Re:Yes, I know how to write programs. How do I *run* them? by xd on Thursday June 30, 2011 @08:27AM (#2713715)
Download a javascript engine that can be run from the command line like v8 or spidermonkey.

This should do you: https://developer.mozilla.org/en/introduction_to_the_javascr...

Edit: Spidermonkey and v8 are packaged up for pretty much any linux distro. Once installed the command to run spidermonkey is `js`, and `d8` for v8. Just add the name of the JS file you wish to run as the first argument.

Re:Yes, I know how to write programs. How do I *run* them? by ColinWright on Thursday June 30, 2011 @08:35AM (#2713733)
Apparently not on my reasonably recent version of Ubuntu. I'll look at installing them. Thanks.
Re:Yes, I know how to write programs. How do I *run* them? by xd on Thursday June 30, 2011 @08:41AM (#2713750)
Downloading the source from github and compiling is easy enough. Either way, good luck and have fun!
Re:Yes, I know how to write programs. How do I *run* them? by ColinWright on Thursday June 30, 2011 @08:52AM (#2713791)
This also works:

  colin@Rrrr:~$ js

  The program 'js' is currently not installed.
  You can install it by typing:

    sudo apt-get install rhino

  colin@Rrrr:~$ sudo apt-get install rhino
(waits 10 seconds)

Done.

Thanks again.

Re:Yes, I know how to write programs. How do I *run* them? by xd on Thursday June 30, 2011 @08:44AM (#2713760)
I just read your original post. Learning to use javascript on the command line isn't the best way to learn to use it for web development. I'd suggest moving on as soon as your comfortable to chrome developer tools or firebug and using the built in console to manipulate web pages.
Re:Yes, I know how to write programs. How do I *run* them? by CyberFonic on Thursday June 30, 2011 @08:24PM (#2716352)
Can do the same with NodeJS (server side), only two steps differ from those in your question:

* Save the code as code.js

* Type "node code.js"

You download additional packages/modules with "npm".

Re:Yes, I know how to write programs. How do I *run* them? by ColinWright on Thursday June 30, 2011 @08:30AM (#2713720)
I've found this: http://www.wooji-juice.com/blog/javascript-article.html

* Is it accurate?

* Is it "the best"?

Re:Yes, I know how to write programs. How do I *run* them? by ColinWright on Thursday June 30, 2011 @12:41PM (#2714732)
Thank you everyone - I've now installed rhino which gives me the js command. Now I'm running in a minimal setup, so I've started.

Thx.

Now I just need to work out the DOM details ...

Re:Yes, I know how to write programs. How do I *run* them? by ColinWright on Thursday June 30, 2011 @08:22AM (#2713695)
Re:Yes, I know how to write programs. How do I *run* them? by iambot on Thursday June 30, 2011 @08:54AM (#2713799)
apart from node.js being for writing javascript servers, would it not do exactly what you're looking for, essentially just a more useful version of the v8 engine with npm for adding useful packages to simplify coding. `$ node yourfile.js`
Re:Yes, I know how to write programs. How do I *run* them? by pewpew on Thursday June 30, 2011 @09:43AM (#2713957)
you fail to understand that javascript was meant to run inside a browser. inside any html page just add script tags and execute your script. you don't need a webserver to run javascript.
Re:Yes, I know how to write programs. How do I *run* them? by ColinWright on Thursday June 30, 2011 @11:06AM (#2714314)
Actually, I do know that JavaScript was originally meant to run in a browser. I also know it can be run "server side," or even just used as a general programming language. My mentioning of the web server was similar to my mentioning of Ubuntu, to try to ensure that the reader knows I'm not a complete computing dweeb.

I also know that one can just embed code between <script> tags, and I know that one can reference code in files elsewhere. But that's inefficient for actual learning. I was asking for a more effective setup without having to download, compile and install huge numbers of extras.

Re:Yes, I know how to write programs. How do I *run* them? by J3L2404 on Thursday June 30, 2011 @12:15PM (#2714604)
See my answer above.
Re:Yes, I know how to write programs. How do I *run* them? by ColinWright on Thursday June 30, 2011 @12:38PM (#2714714)
I've now installed rhino which gives me the js command. Now I'm running in a minimal setup, so I've started. Thx.
Re:Yes, I know how to write programs. How do I *run* them? by J3L2404 on Thursday June 30, 2011 @09:27AM (#2713905)
http://jsfiddle.net/

Or

If you just want to manipulate others web pages it can be done by pasting into your browser's address bar.

javascript: code here

To make a bookmarklet paste into any bookmark address.