Creating a node.js (JavaScript) script/program

In this class we run JavaScript programs via node.js which allows us to run JavaScript as command line programs. This gives us an environment which is similar to those used by C or C++ which will be learned in later CS courses, while potentially being a bit easier for the beginner.

A node JavaScript program is just a text file that begins with the following first line:

#!/usr/local/bin/node

and the remainder of the text file is the JavaScript program itself. The characters #! inform the operating system that the program that follows should interpret the text that follows. /usr/local/bin/node is then the path to the node interpreter. An interpreter is simply a program that reads source (the program you write) and "interprets" it by carrying out the instructions laid out by the source.

What follows is a simple program example. It is often referred to as a "Hello, world!" program. A program traditionally written first by someone learning a new programming language.

#!/usr/local/bin/node console.log("Hello, world!");

The above text can be saved into a text file and then made into an "executable" JavaScript program (i.e. it can be "run", just like any other Unix command, such as ls or cp or your favorite editor (i.e. kate)) by setting the execute permissions on the text file.

Supposing the above was saved as the file hello.js, we can make it executable by performing the following "chmod" command, and then we can run it

cs15199@cs:~/> chmod a+rx hello.js cs15199@cs:~/> ./hello.js Hello, world! cs15199@cs:~/>

Let us now deconstruct the one and only statement -- a command or instruction to the computer to be carried out. In our "Hello, world!" program, reading it from left to right: First is the word "console". console is a JavaScript object that represents a collection of properties (think names and values) and methods (often called functions or smaller, self contained programs within the larger program) that relate to outputting text to the console or in our case, the terminal window. The next word "log" refers to one of the methods within the console object that outputs its input (which we'll get into shortly) to the console. There are other methods, such as "error", "warn" and "info" which more or less do the same thing in our case, but if you ever use JavaScript in a browser, allows the system to route the messages differently.

Your browser has a console, similar to your terminal window. It can be accessed usually by pressing the key combination Ctrl-Shift-j or Ctrl-Shift-k. You might also be able to access it from your browsers menus via Tools → Web Developer → Web Console or the like.

The next item of interest are the ()'s that contain the string "Hello, world!". The () are the "call" operator and the data inside of it, separated by commas are the parameters to the function call. So the string "Hello, world!" is passed to the log function in the console object to be printed to the console log.

And with that we have made our first JavaScript program.