Your first program
Problem
The computer does exactly what it says
The computer does not guess what you want. It runs commands, one after another, exactly as they are written. A sequence of such commands is a program.
Here is a whole program. It prints two lines on the screen:
console.log("Welcome!");console.log("This is your first program.");Welcome!This is your first program.This is the whole program: two lines, one command per line. There is no wrapper around them. You save them in a file called hello.js and run it.
The idea
What is in the program
- The program is the file itself. It is run by Node.js: the program that runs JavaScript outside the browser. It goes through the lines from top to bottom, from the first to the last.
console.log(...)prints the text in the parentheses and moves to a new line.- The text is in double quotes:
"Welcome!". The quotes are not printed. - Every command ends with
;. JavaScript often manages without it, but we always write it: that way you can see where a command ends.
You run the program from the terminal, in the file's folder:
node hello.jsNode.js reads the file and runs the commands in it. There is no separate step where the code is translated ahead of time: you write and you run.
More precisely, if you are curious
- Uppercase and lowercase letters matter.
Console.logis not the same asconsole.log, and the program stops with an error when it reaches that line.
- Text can also be in single quotes:
console.log('Welcome!');prints the same thing. In this course we write double quotes. - JavaScript also runs in the browser. There
console.logprints to the console of the developer tools (F12). In this course we run the programs with Node.js.
The idea
Printing with and without a new line
Sometimes you want several pieces of text on one line. console.log always moves to a new line, so there is another command for that: process.stdout.write. It prints the text and stays on the same line.
process.stdout.write("Order ");process.stdout.write("#1024");console.log(" is confirmed.");console.log("Delivery: tomorrow");Order #1024 is confirmed.Delivery: tomorrowThe first two lines of the code use process.stdout.write, so "Order", "#1024" and "is confirmed." come out together. After console.log the next text starts on a new line.
Pay attention to the spaces. They are inside the quotes, because the computer does not add a space by itself.
Follow the cursor: the text always comes out where it is.
process.stdout.write("Order ");process.stdout.write("#1024");console.log(" is confirmed.");console.log("Delivery: tomorrow");Step by step, in words
- process.stdout.write prints "Order " and the cursor stays on the same line.
- process.stdout.write prints "#1024" and the cursor stays on the same line.
- console.log prints " is confirmed." and the cursor moves to a new line.
- console.log prints "Delivery: tomorrow" and the cursor moves to a new line.
- The spaces are inside the quotes: the computer does not add a space by itself.
More precisely, if you are curious
process.stdout is the program's output in Node.js. The browser does not have it: there the text for one line is collected and printed all at once with console.log.
Try it
On how many lines?
What will the program print?
process.stdout.write("Train ");console.log("8:15");process.stdout.write("Platform ");process.stdout.write("3");console.log();After "8:15" there is console.log, so "Platform" is on a new line. The last line, console.log();, prints no text. It only moves to a new line.
The idea
Comments: notes for people
Sometimes you want to leave a note in the code: why it is like this, or what comes next. You write it as a comment. The computer skips comments.
//starts a comment that runs to the end of the line./*and*/surround a comment over several lines.
/* The coffee shop receipt. Prices are in euros. */console.log("Coffee €2.50");// console.log("Croissant €1.80");console.log("Total €2.50"); // out of croissantsCoffee €2.50Total €2.50The croissant line is code, but it is behind //, so it does not run. This is how you switch off a line for a while without deleting it.
A good comment says why, not what. The code itself shows what it does.
Try it
What will come out?
What will the program print?
console.log("9:00 Math");// console.log("10:00 Physics");console.log("11:00 History"); // lunch is nextThe whole second line is a comment, so "Physics" is not printed. On the third line the comment starts after `;`: the command runs, and the note after // is skipped.
Problem
The program does not start
You wrote the program, you run it, and it does not even begin. Not a single line comes out. Instead of output you get a message:
console.log("Welcome to the course!"console.log("Here we go.");/home/student/welcome.js:1console.log("Welcome to the course!" ^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: missing ) after argument listHere the closing parenthesis is missing on the first line. Before it runs the first line, Node.js reads the whole file and checks that it is written by the rules of the language. If it is not, it stops and runs nothing, not even the lines before the error. This is called a syntax error (SyntaxError).
The idea
How to read the message
The message tells you three things: the file, the line and what is wrong.
/home/student/welcome.js:1 means the file welcome.js (with the full path to it), line 1. Under it is the line itself, and the ^ marks underline the place where Node.js noticed the problem. Then comes the kind of error and what is wrong: missing ) after argument list means "a ) is missing after the things in the parentheses".
Under the message Node.js prints more lines that start with at. They say where inside Node.js itself the error was noticed. Skip them for now.
There is a second kind of error too. The program starts, runs the lines one after another and stops only at the line it cannot run:
console.log("Welcome to the course!");Console.log("Here we go.");Welcome to the course!/home/student/welcome.js:2Console.log("Here we go.");^
ReferenceError: Console is not definedConsole with a capital letter is a name Node.js does not know (ReferenceError). It finds out only when it reaches that line, so the first line has already been printed. Again it gives the file and the line, and then the kind of error.The most common errors at the start:
- a quote or a parenthesis is opened but not closed (SyntaxError);
- an uppercase letter instead of a lowercase one:
Console.log(ReferenceError, when the program reaches the line).
Node.js shows only one syntax error at a time. You fix it, run again, and only then you see the next one, if there is one.
More precisely, if you are curious
- Node.js tells you where it noticed the problem. Sometimes the error is a little higher up.
- There are also errors that nobody catches: the program starts, but it does something other than what you want. We will hunt those in almost every lesson.
Try it
Find the bug
The program should print the exam schedule. It does not start: not even the first line comes out. Which line is the bug on?
Pick the line with the bug.
); as part of the text too and reaches the end of the line with no closing quote: SyntaxError: Invalid or unexpected token ("a character I did not expect"). That is why not even "Time: 10:30" from line 1 comes out. The fix is console.log("Room: 204");.Exercise
Your first programs
Solve the tasks in your editor. Each one is a separate program. Compare your output with the sample, character by character.
1. Hello World. The traditional first program. Print:
Hello, World!2. Business card. Print a business card of three lines: name, job and email. You can use your own.
Maria Ivanovaweb developermaria@example.com3. Frame. Print today's cafeteria menu in a frame of asterisks. Watch the spaces, so the right side comes out straight.
**************** Today: soup ** €5.40 ****************More tasks
- One sentence in three parts. With three
process.stdout.writecommands and oneconsole.log, print on one line "The shop is open from 9:00 to 19:00." - A schedule with a comment. Print a schedule of four classes. Then switch off the third line with
//, without deleting it, and add a comment saying why.
- An error on purpose. Remove the
;from a line in task 1: the program still works. Then remove a parenthesis, then a quote, then writeConsole.logwith a capital letter. Read what Node.js says each time.
Test