Free lesson · What JavaScript Is, and Running Your First Program

Reading an error message

You are going to see far more errors than working programs, especially at first. That is not a sign you are doing badly; it is the job. What separates people who progress quickly is that they read the message instead of panicking at the colour red.

Make a mistake on purpose. Put this in a file and run it:

console.log(greeting);

Node answers something close to this:

ReferenceError: greeting is not defined
    at Object.<anonymous> (/Users/you/js/hello.js:1:13)

Three separate pieces of information are in there, and each is useful.

  • ReferenceError — the kind of problem. This one means a name was used that nothing was ever attached to.
  • greeting is not defined — the specific detail. It even names the culprit.
  • hello.js:1:13 — the file, the line, and the column. Line 1, character 13.

The error kinds you will meet first

  • SyntaxError — the text is not valid JavaScript at all, usually an unclosed bracket or quote. Nothing ran, not even line 1.
  • ReferenceError — a name was used that does not exist. Frequently a typo.
  • TypeError — the value exists but cannot do what you asked, such as calling something that is not a function.

The difference between the first and the other two matters. A SyntaxError stops everything before a single line executes, so if you see output before the error, it cannot be a syntax problem.

Read the first error, not the last. One genuine mistake often produces several complaints, and the ones after the first are usually consequences. Fix the top one and re-run before reading further.

Try these

  1. Cause a SyntaxError deliberately — leave a quote unclosed. Does anything print before it?
  2. Cause a TypeError by writing const x = 5; x();. What does the message say the problem is?
  3. Put a console.log before and after a deliberate error. Which of the two appears?

This is one lesson of 60

JavaScript Foundations: The Language, Explained Properly continues from here — 4 lessons are free to read like this one, and the rest come with the course. Enrolled readers also get an AI tutor that has read the lesson they are on.

See the full course