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
- Cause a
SyntaxErrordeliberately — leave a quote unclosed. Does anything print before it? - Cause a
TypeErrorby writingconst x = 5; x();. What does the message say the problem is? - Put a
console.logbefore and after a deliberate error. Which of the two appears?