A program is a list of instructions for a computer, written as text. JavaScript is a programming language: a set of rules for writing those instructions so that a computer can follow them. To run a program is to ask the computer to carry the instructions out.
JavaScript began as the language of web pages, and it also runs outside the browser. This unit uses Node.js (often shortened to Node), a free tool that runs a JavaScript program on its own and shows the text it produces. The programs you write here are console programs: they print lines of text and, from lesson 3, read lines of text.
You do not need to install anything for these lessons. Every example with an Edit & run button runs right here on the page, and you can change it and run it again. Nothing you type can break anything.
One instruction, one line on the screen
Here is a complete JavaScript program. Run it.
console.log("Hello, world!");It shows Hello, world! on the screen. The text a program shows is called its output.
The parts of that one line have names you will meet in every lesson:
console.logis a function: a named action that JavaScript already knows how to do. This one shows a line of text.consoleis the place where a program writes its text, and.logpicks the "write a line" action that belongs to it.- The round brackets after the name call the function, which means "do that action now".
"Hello, world!"is a string: a piece of text. The quotation marks tell JavaScript where the text starts and stops. They are not part of the text, so they are not shown.- The semicolon
;marks the end of the instruction, as a full stop ends a sentence.
The value you put between the brackets is called an argument. It is what the function works on: here, the text to show.
console.log puts a line on the screen.Statements run from top to bottom
A statement is one complete instruction. Write one statement per line and end it with a semicolon. A program with three statements does three things, always in the order they are written: first line first.
console.log("Fill the kettle");
console.log("Boil the water");
console.log("Make the tea");This prints three lines, in the same order as the statements:
Fill the kettle
Boil the water
Make the teaStep through the diagram to watch each line run. Then swap two lines in the program above and run it again: the output changes order too, because Node never looks ahead or rearranges your statements.
console.log starts a new line on the screen.console.log() with nothing between the brackets prints an empty line. That is useful for spacing output out.
Comments are notes for people
A comment starts with //. JavaScript ignores everything from the // to the end of that line. Comments are for the person reading the program, including you next week.
// Show a two-line welcome.
console.log("Welcome!");
console.log(); // an empty line
console.log("Come in.");This prints Welcome!, then an empty line, then Come in.. The comments print nothing.
A // inside the quotation marks of a string is ordinary text, not a comment: console.log("Read // twice"); prints Read // twice.
Exactness matters
JavaScript follows your text exactly, so small differences change the result:
- Capital letters count.
console.logworks;Console.logandconsole.Logare different names that JavaScript does not know. - Everything between the quotation marks is printed exactly, including spaces and punctuation.
"Hello,world!"and"Hello, world!"are different strings. - Each string needs a quotation mark at both ends, and each call needs both brackets.
Read your first error message
Everyone who programs sees error messages every day. An error message is not a telling-off. It is Node saying which line it could not follow, and why. Learning to read one is a skill in its own right.
This program has a mistake: the closing quotation mark is missing.
console.log("Hello, world!);Node refuses to run it and reports:
main.js:1
console.log("Hello, world!);
^^^^^^^^^^^^^^^^
SyntaxError: Invalid or unexpected tokenRead it in three steps:
- The first line says where.
main.jsis the name this site gives your program, and the1after the colon is the line number. - The next two lines show the place. Node repeats the line and puts
^marks under the part it could not read: here, a string that starts and never ends. - The line that starts with an error name says what is wrong.
SyntaxErrormeans the text breaks JavaScript's writing rules, so Node could not even start. "Invalid or unexpected token" means Node met something it could not make sense of. A token is one piece of program text, such as a name, a bracket or a string.
A second very common mistake is a misspelt name:
consle.log("Hello, world!");This time the text follows the writing rules, so Node starts running it and stops at the line it cannot carry out:
/work/main.js:1
consle.log("Hello, world!");
^
ReferenceError: consle is not defined
at Object.<anonymous> (/work/main.js:1:1)ReferenceError means the program refers to a name that JavaScript does not know. This time the file name has /work/ in front of it, which is the folder on our server where the program was running. Again the first line gives the line number, the ^ marks the spot and the error line says what is wrong. Below it Node prints several lines that start with at. The first of them repeats the position (main.js:1:1 is line 1, character 1); the others describe Node's own workings, and you can ignore them.
When you see an error: find the line number, look where the ^ points, read the line that names the error, and compare that spot with a working example.
Running JavaScript on your own computer
This is optional; the lessons do not need it. If you install Node.js from nodejs.org, you can save a program in a text file named hello.js, open a terminal (a window where you type commands) in the same folder and type node hello.js. The output appears in the terminal, the same as it does on this page.
What you can do now
You can write a program of several console.log statements, predict the order of its output, add comments, and find the line an error message is pointing at. In the exercises you will predict what small programs print, repair two broken ones and then write a three-line program of your own.
Next: name values with variables, so that a program can remember and calculate things.