1

I am trying to teach myself JavaScript, and I am doing so by editing an existing program's files, but I would like to write a script, (not HTML) and run it, and see the console results to see what's going on instead of trying to get the output via odd ways.

Is there such a thing? I guess I am thinking like batch files where you can write one and then when it runs see what it is doing. I would also like to get console.log type output. Any ideas?

I am only interested in things like 2+2 = 4 – not trying to interface with anything.

I am just looking for a way to console.log my script. I am not looking for libraries or custom variants of JavaScript engines.

Is there really no way to make a pure JavaScript that says 2+2 = and run it in a console window?

1
  • 1
    I don't know enough about it to make an answer, but I'd browse around NODE.js tools. They should have something. Jun 12, 2014 at 20:22

4 Answers 4

2

You can certainly run JavaScript in a "normal" terminal, using a JavaScript interpreter.

V8 is Google's JavaScript engine, and you can download it from SVN, compile it, then run it in a terminal:

sudo apt-get install subversion scons libreadline-dev
svn co http://v8.googlecode.com/svn/trunk v8
cd v8/
scons console=readline d8

Building is also explained on the Google Code page. It comes with d8, a command-line JavaScript interpreter.

Then all you need is to write a file, containing print(2+2), and then run it through V8:

$ d8 /path/to/file.js
4

See also: Sandeep's blog: Using the V8 javascript shell (D8)

1
  • hey thanks, I found something called jsdb but I have not figured out how to run it yet. I will try yours here now
    – Donald
    Jun 12, 2014 at 18:42
1

Firefox has plugins that give you a JavaScript console to test with. You could run it on Linux.

Standalone JavaScript shells

7
  • I am a windows 8 user. will it still function, and will it run loops, I found jsfiddle, but it does the loops one at a time. edit: you did not mean use firefox or linux did you?
    – Donald
    Jun 12, 2014 at 18:05
  • There is a few plugins you need to test them i suspect one of them will run loops but be warned be careful or infinite loops it will just about kill your computer and you need to do force restart with power button Jun 12, 2014 at 18:08
  • Infinite loops in JavaScript will kill a computer? That's news to me.
    – slhck
    Jun 12, 2014 at 18:22
  • I don't mean literally, i mean if the loop is bad it can cause the memory and cpu usage to hit 100% which can make it hard to kill manually as the whole computer goes slow Jun 12, 2014 at 18:24
  • @AndrewCrawford - Just kill the Firefox process....A forced shutdown isn't required.
    – Ramhound
    Jun 12, 2014 at 18:35
0

JavaScript is intended to manipulate web documents. I don't see the point of trying to learn JavaScript without the web part. But it's an interesting method. Though its syntax is very related to Java, almost the only part that changes are the DOM classes and names.

You may want to take a look at JSFiddle or similar. It allows you to have a live preview of HTML, CSS and JavaScript and see how they work together right on your browser. Many people here uses it to show examples of code snippets and things like that.

Update:

Here is a cleaner version of the fiddle in the comments which may serve well for what you want, without having to install anything locally. Of course this wouldn't be the way to go if you want an off-line console.

You need to use a container to actually display the output of the JavaScript, like how I used the Test object.

Test.innerHTML += i + '<br/>';

This is the closest you can get to a console on JSFiddle.

16
  • thats what I am using now, but if I write a code that has loop in it, I have to click for each loop, I am not liking this because it causes me to have to make my loops smaller just to run the scripts, And I am learning this for the sake of learning it, and also to edit files in a program I already have, I have no desire to build web pages. Seems as if enough of thats going on LOL.
    – Donald
    Jun 12, 2014 at 18:57
  • I don't get the part of having to click for each loop. A loop is a loop, JSFiddle doesn't stop loops every time they repeat.
    – arielnmz
    Jun 12, 2014 at 19:06
  • Check this fiddle, I didn't have to press the anchor everytime I wanted to print a number…
    – arielnmz
    Jun 12, 2014 at 19:17
  • its funny you say that, because just as I came here to tell you guys I found a standalone jsdb.org that seems to do what I want, I also, realized the reason my loops are not working is that I put the -- on the wrong side, LOL. learning, its hard sometimes.
    – Donald
    Jun 12, 2014 at 19:48
  • Your original question didn't mention you want a solution for databases in Javascript, but I'm glad you found something that fits your needs. By the way, you should do more research and code troubleshooting before asking a question.
    – arielnmz
    Jun 12, 2014 at 19:55
0

Check out NodeJS, it allows you to run JavaScript outside of a browser. You can also use Nacre.sh, which is a JavaScript Shell based on NodeJS that provides a set of practical functions such as ls, cd, chmod, etc.

Here are some examples:

> 1 + 1
2
> [1, 2, 3, 4].map(e => e * e)
[ 1, 4, 9, 16 ]
> ls()
[
  'Applications',
  'Desktop',
  'Documents',
  'Downloads',
  'Library',
  'Movies',
  'Music',
  'Pictures'
]

Let's say you want to run the following JS script:

// myscript.js
const you = 'World!';
console.log('Hello', you);
# use nacre or node depending on if you want the bash ls, cd, etc. functions
nacre ./myscript.js 
Hello World!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .