This first post is to get you setup with some of the tools you’ll need to make and then run your programs.
If you’re on Windows you’ll have a nice and easy installer that you can run. Just download the appropriate file for your system and follow the installation.
When running the Windows installer I’ve only run into one problem: node not being properly included into the path after install. If this happens you can’t run node from anywhere in the powershell or the command prompt, which can be quite frustrating.
The remedy is to find where node is installed on your system and then add it to the path. The exact process to do this changes depending on which version of Windows you are running. </small>
Things are a bit different if you’re on a Linux system, and depending on your distribution installing node might just be as easy as:
sudo apt-get install -y nodejs
If your distribution doesn’t already have node in it’s repositories, then you might want to follow these instructions on Github to help you get up and running.
The terminal, the command prompt, powershell, that monolithic thing with its one blinking eye. It goes by many names, but it is basically a text based interface between you and your computer. And, while it might be daunting for some, you’ll have to get to know the console well to use node and the programs we’ll be making. If you want to get a bit more familiar with the console, feel free to check out my post Your friend, the Terminal. Oh, and if you’re on Windows, use powershell.
So now that you have your tools and they’re setup, let’s get ready to write our first program. The purpose of this program will be simple, to output the words “Hello World!” to the screen.
/* jshint node: true */ 'use strict'; console.log('Hello World!');
When you’re done save your program to your computer. Name it something you’ll
remember or just
app.js, it doesn’t matter too much.
Then, open up your console and
cd to the directory where you stored your
program. Then, run the
node command followed by your program’s name to execute
it. If your program was named
app.js, all of that that together should look
something like this:
joel@monolith:~$ cd Documents/node_projects joel@monolith:~/Documents/node_projects$ node app.js Hello World!
If all went well, the words “Hello World!” should have been outputted to the console. If so, hurray! You just wrote your first working program. But what’s actually going on here though? There are a few different parts here that make this program work. I’m going to break it down and explain them to you.
The first couple lines of the program don’t handle any of the executing of the program, but are instead there to help instruct the code parser on how to evaluate our code.
/* jshint node: true */
After that comes the real meat of the program:
This line is made up of three distinct parts. The first,
the built-in console object. Objects are a fundamental part of modern
programming, and can be described as self-contained modules that can either do
things or store values. The
console object is a built-in object that can
handle some of the outputting of data to the console.
After we specify the console object, we’re calling the conole object’s
method. A method is a name for something that an object can do. The syntax to
access an object’s methods is a period followed by the method name,
Just accessing an object’s methods isn’t enough though, to actually run the
method you have to follow it with parenthesis,
(). Methods can also take in data
they need to execute properly, and the way that is done is by passing the value
) two parenthesis. In our case, we pased in the string-literal,
All together this tells the console’s log function to go ahead and output the value “Hello World!” to the console.
Then, at the very end, we have a single semi-colon,
best practice to end all of your statements with a semi-colon. It lets the
interpreter know it is ok to move on to the next thing to execute. It’s the same
as in writing, where you end all of your sentences with a period. If you didn’t
you wouldn’t know where one sentence ends, and another begins.
Now that you’ve written your first program, and gotten a run down of how it works, why not do some experimenting to see what you can do with things?
- Can you rewrite your program to make something different be output?
- What happens if you call `console.log` more than once in the same program?
- Does the space between different parts of our programs matter?