Starting with javascript

November 4, 2011 at 05:45:46
Specs: Windows 7
I am starting to play with javascript. I am just wanting to run a 'Hello World' example from the command prompt (windows). I have saved the following three lines as Hello.js, and am simply trying to run this, so that it will print 'Hello World'.

function helloWorld() {
   alert('Hello World!') ;

However, nothing is printed when I type at the command prompt these options:
cscript Hello.js
wscript Hello.js

(double clicking the .js file make the cmd.exe open and close VERY briefly.
When I add an error by inserting a random bracket somewhere, it does complain about this. Can anyone point me in the right direction and help me to get started?

See More: Starting with javascript

Report •

November 4, 2011 at 06:27:54
First of all, you need to get the name right. If you're going to be using the cscript / wscript script engines, then you're using JScript, MS' branding of JavaScript. JScript does certain things JavaScript cannot, and it runs in environments (namely outside of a DOM) JavaScript cannot. Basically, if you're making websites (and your browser tests include more than just IE), you're writing JavaScript. If you're automating Windows, it's JScript.

Now, to answer your question, you need to either remove the function declaration, or add a call to the function, outside of any other functions. Something like this:

function helloWorld() {
   alert('Hello World!') ;

How To Ask Questions The Smart Way

Report •
Related Solutions

Ask Question