Lesson 5: Reading errors and developing a programming mindset
01Opening question
When red text appears in the console, is it a disaster, or the most useful clue you have?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
Run it. Read the three parts of the error: its name (ReferenceError), its message and its line number. Then fix the typo.
const greeting = "Hello";
console.log(greting);
03Objective and prerequisites
Learning objective: Read an error message (type, message and line), tell syntax errors from runtime errors, and fix bugs with a repeatable reproduce–locate–inspect–change–verify loop.
Errors are not failures: they are the computer telling you exactly where it got stuck. Every error message has three useful parts: the type (such as ReferenceError), the message and the line number.
A syntax error means JavaScript could not even read your code, so nothing ran. A runtime error happens while the program is running: lines before the error have already run, lines after it have not.
The most common beginner errors are typos in names (ReferenceError: ... is not defined), using a value as a function (TypeError: ... is not a function) and missing quotes or brackets (SyntaxError).
Use the programmer's loop: reproduce the problem, locate the line, inspect the values with console.log, change one thing, and verify by running again. Changing many things at once makes it hard to know what helped.
Errors are objects with name, message and (non-standard but universal) stack properties. Built-in types include SyntaxError, ReferenceError, TypeError, RangeError, URIError and EvalError, plus AggregateError; all inherit from Error.
SyntaxErrors are early errors raised while parsing, before evaluation. ReferenceErrors arise when resolving an unbound identifier (or accessing a let/const binding in its temporal dead zone). TypeErrors arise when an operation is applied to a value of the wrong type.
An uncaught exception unwinds the call stack and terminates the current job; in a browser, other event handlers still run later. Stack traces list the call chain from the innermost frame outwards, which is how you trace an error back to its cause.
Effective debugging is hypothesis-driven: form a specific explanation, design an observation that would confirm or refute it, and keep a record. Lessons 102 and 116–120 extend this with developer tools and regression tests.
05Definitions
Error message
Text that describes what went wrong, shown with the error type.
SyntaxError
The code breaks a grammar rule, so JavaScript cannot read it and nothing runs.
ReferenceError
The code uses a name that does not exist (often a typo).
TypeError
A value was used in a way its type does not allow, such as calling a number like a function.
RangeError
A value is outside the allowed range, such as a negative repeat count.
Runtime error
An error that happens while the program is running, after earlier lines have already run.
Bug
A mistake in a program that makes it behave differently from what was intended.
An order summary prints the item count and then crashes with ReferenceError: total is not defined, even though the developer is sure they created the total.
A customer reports that the order page 'stopped working'. A professional does not start rewriting code: they reproduce the problem, read the error in the console, find the line, inspect the values and make one change at a time, then confirm the fix with a test so the bug cannot quietly return. The Debugging Detective Laboratory gives you cases to practise exactly this.
// 1 Reproduce 2 Locate 3 Inspect 4 Change one thing 5 Verify
console.log("Investigation plan ready");
12Knowledge check
1. ReferenceError: totl is not defined most likely means:
totl holds the wrong number
A name is misspelled or was never declared
The file is missing
The browser is out of memory
2. Why does a SyntaxError on line 10 stop line 1 from running?
Line 1 also has an error
JavaScript reads the whole program before running any of it
Errors run backwards
It does not; line 1 runs
3. What is printed before the program stops?
console.log("a");
const n = 5;
n();
console.log("b");
Nothing
a
a and b
b
4. Your total is wrong but no error appears. What is the best first step?
Rewrite the whole program
Log each intermediate value with a label
Ask an AI to rewrite it without checking
Add more features
5. Which error does this produce?
const price = 10;
price = 12;
SyntaxError
ReferenceError
TypeError: Assignment to constant variable
No error
6. In the debugging loop, why change only one thing at a time?
It is faster to type
So you know which change fixed (or broke) the behaviour
JavaScript only allows one change per run
To keep the file short
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the typo: Prints the message; Runs without errors
Make the program run: Prints items and total; Runs without errors; total is still calculated
An error-free score report: Prints average and highest; Scores are stored in constants; Uses Math.max; Runs without errors
ex-005-01: output matches the documented expected output
ex-005-02: output matches the documented expected output
ex-005-03: output matches the documented expected output
ex-005-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-01-discovering-javascript/lesson-005-reading-errors-and-developing-a-programming-mindset/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
Every error has a type, a message and a line: read all three.
Syntax errors stop everything; runtime errors stop the program at the failing line.
Silent logic bugs need investigation with labelled logs.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
DebuggingCoach me through an error
Context: My JavaScript shows an error I do not understand.
Task: Ask me for the error name, message and line, then help me investigate without giving me the fix immediately.
Constraints: Use the loop: reproduce, locate, inspect, change one thing, verify. One question per message.
Expected behaviour: I fix the bug myself and can explain the cause.
Testing: At the end, ask me to describe the root cause in one sentence.
Concept explanationSyntax versus runtime errors
Context: I am learning about JavaScript errors.
Task: Explain the difference between syntax errors and runtime errors with two tiny examples of each.
Constraints: Examples must be three lines or fewer.
Expected behaviour: I can predict which lines run before each error.
Testing: Ask me what prints for each example before explaining.
Code reviewIs this AI fix safe?
Context: An AI suggested a fix for my ReferenceError.
Task: Help me check whether the suggestion fixes the cause or only hides the symptom.
Constraints: Ask to see the original code, the error and the suggested change.
Expected behaviour: I can explain why the fix is correct, or why it is not.
Testing: Suggest a test that would fail before the fix and pass after it.
Exercise generationError-reading practice
Context: I can read ReferenceError, TypeError and SyntaxError messages.
Task: Create four tiny broken programs, each with a different error, for me to fix.
Constraints: Each under five lines; tell me the expected output after fixing.
Expected behaviour: Each program has exactly one bug.
Testing: List the expected output so I can verify my fixes.
Guided learningBuild a debugging habit
Context: I tend to change lots of code when something breaks.
Task: Give me a short checklist and a practice routine to build a disciplined debugging habit.
Constraints: Practical, under 200 words.
Expected behaviour: I follow the same steps every time.
Testing: Give me one practice bug to try the checklist on.