JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 1: Discovering JavaScriptLesson 5 of 150About 45 minutes

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.

You need: Lesson 4: Using the console and understanding output

Skills practised: Debugging, JavaScript basics.

04Explanation

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.

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.

Open the full glossary

06Visual learning object

Error message decoder. Pick or type an error message and see its parts, what it means and a plan for investigating it.

Loading the interactive object…

07Examples

ex-005-01A ReferenceError Browser-executable JavaScript

The first log runs. The second uses a misspelled name, so it stops with a ReferenceError and the last line never runs.

const total = 120;
console.log("Before the error");
console.log(totl);
console.log("This line never runs");
Sample input
None
Expected output
Before the error Uncaught ReferenceError: totl is not defined
Common mistake
Fixing the line that the error mentions in a different way (for example declaring totl) instead of correcting the typo.
Try this
Fix the typo and predict the full output before running.
Source
examples/ex-005-01-reference-error.js

ex-005-02A TypeError Browser-executable JavaScript

price holds a number. Adding () tries to call it as a function, which numbers cannot do.

const price = 25;
console.log("Price:", price);
price();
Sample input
None
Expected output
Price: 25 Uncaught TypeError: price is not a function
Common mistake
Adding () after a variable out of habit. Only functions are called with ().
Try this
Remove the bad line and add a line that prints price * 2.
Source
examples/ex-005-02-type-error.js

ex-005-03A SyntaxError stops everything Browser-executable JavaScript

Line 2 is missing its closing parenthesis. JavaScript cannot read the program, so not even line 1 runs.

console.log("Line 1 is fine");
console.log("Line 2 is missing a bracket";
console.log("Line 3 is fine");
Sample input
None
Expected output
No output: SyntaxError on line 2
Common mistake
Looking only at the line before the one reported. Check the reported line first, then the line above it.
Try this
Add the missing ) and run again.
Source
examples/ex-005-03-syntax-error-stops-everything.js

ex-005-04Investigate with logs Browser-executable JavaScript

No error appears, yet the result is wrong. Logging each step reveals that only the discount was divided by 100.

// Goal: the price after a 20% discount should be 80.
const price = 100;
const discount = 20;
const finalPrice = price - discount / 100;
console.log("discount / 100 =", discount / 100);
console.log("finalPrice =", finalPrice);
const fixedPrice = price - price * (discount / 100);
console.log("fixedPrice =", fixedPrice);
Sample input
price 100, discount 20%
Expected output
discount / 100 = 0.2 finalPrice = 99.8 fixedPrice = 80
Common mistake
Assuming no error message means the program is correct. Logic bugs are silent.
Try this
Change discount to 50 and predict fixedPrice.
Source
examples/ex-005-04-investigate-with-logs.js

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Fix the typo

Read a ReferenceError and use it to locate a typo.

const message = "Errors are clues";
console.log(mesage);

Make the program run

Fix a syntax error and a runtime error, one at a time.

const itemPrice = 15;
const quantity = 4;
const total = itemPrice * quantity;
console.log("Items: " quantity);
console.log("Total:", Total);

An error-free score report

Build a small program and verify it runs cleanly.

// Test scores: 78, 85 and 89.

09Prediction

Commit to an answer first, then run the real code to compare.

console.log("start");
console.log(5 + undefinedValue);
console.log("end");

What happens?

10Debugging investigation

total is not defined

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.

const Total = 135;
const itemCount = 6;
console.log("Items:", itemCount);
console.log("Total:", total);

More cases in the Debugging Detective Laboratory

11Practical scenario

A support ticket

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

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

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.
  • Reproduce → locate → inspect → change one thing → verify.

16AI learning prompts

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.

Debugging Coach 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 explanation Syntax 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 review Is 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 generation Error-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 learning Build 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.