Skip to content
Theory · First program · lesson 2 of 7

Errors in a program

In the previous lesson, the program stopped at the word Hello without quotes. Let's go back to the cook and the recipe: a cook can stop for two different reasons.

  • Before starting work, the cook reads the whole recipe and hits a phrase that makes no sense, for example one cut off halfway. Then the cook does not follow a single step.
  • The cook is already cooking and reaches the step "add the sauce", but there is no sauce in the kitchen. The steps before it are done, and the work stops there.

JavaScript has errors of both kinds. Let's start with the second one: a program can stop halfway through. The second line of this program holds the name total, and the program never says what it means:

JavaScript
console.log("start"); // start
console.log(total); // ✗
console.log("end");
TEXT
ReferenceError: total is not defined

The program ran like this:

  1. The first line printed start.
  2. The second line stopped the program with an error.
  3. The third line is written correctly, but it did not run, and end is not printed.

Runtime error

A ReferenceError is a runtime error: Node.js finds it only when it reaches the line with it. The lines above it have already run by then, and the lines below it will not run.

JavaScript neither puts the empty value undefined in place of an unknown name nor skips the line. It stops the program. Here the cook picture stops working: a real cook would skip the step or replace the sauce with something else.

The message about a runtime error always reports only the first such error: the program never reaches the later ones.

Syntax error

Node.js finds errors of the other kind before the program starts. Before running a file, it reads all of it and checks that the text follows the language's rules of writing. If, for example, a parenthesis is not closed, not a single line runs, not even the first:

JavaScript
console.log("start");
console.log("end"; // ✗
TEXT
SyntaxError: missing ) after argument list

This is a syntax error, and this time not even start is printed. Most often a syntax error comes from:

  • an unclosed parenthesis;
  • an unclosed quote;
  • a missing comma between values.

Telling one from the other

Interviewers often ask: "How does a syntax error differ from a runtime error?" The difference is when Node.js finds the error:

  • a syntax error before the program starts, so not a single line runs;
  • a runtime error when the program reaches its line, so the lines above it have already run.

The output shows which of the two errors happened: if the first lines managed to print something, it is a runtime error.

Reading the message

Node.js prints several lines about an error:

  1. The path to the file and the number of the line with the error, for example errors.js:2.
  2. The line of code itself, with ^ marks under it that point at the place of the error.
  3. The main part: the error's name and its description, ReferenceError: total is not defined.
  4. Lines that start with at show where inside Node.js the file was running. Your first programs can skip them.

The words is not defined mean that the program never said what this name means. The course lessons keep only the line with the name and the description, as in the examples above.

Common mistake

JavaScript tells capital letters from small ones. To it, Console and console are two different names, and the program never defined the first one:

JavaScript
Console.log("Hello"); // ✗
TEXT
ReferenceError: Console is not defined

The fix is to write the name exactly as in the examples: console.log.

Key points

  • Node.js finds a syntax error before the program starts, and not a single line runs.
  • Node.js finds a runtime error, such as ReferenceError, only at its line: the lines above it have already run, and the ones below have not.
  • The main line of the message is the error's name and description, for example ReferenceError: total is not defined.
  • JavaScript tells capital letters from small ones, so Console and console are different names.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way