Lesson 5 of 28
In progress
The console and reading errors
Use the console deliberately, read the three errors beginners meet most, and debug by narrowing instead of guessing.
An error message names the file, the line and the kind of problem. Most beginners lose time not because the error is cryptic, but because they never read past the word "Error".
What you will learn
- Use console methods beyond
log. - Recognise ReferenceError, TypeError and SyntaxError.
- Debug by narrowing the failing code.
More than console.log
Editable. Runs in your browser.
console.log(items);
console.table(items);
console.warn("Low stock");
console.error("This is an error message, not a thrown error");
console.log("Items:", items.length);
Press Run to see the output.
console.table prints arrays of objects as a table, which is far easier to scan than nested braces. console.warn and console.error colour the output and can be filtered. console.error does not stop the program; it only logs.
console.log("label:", value) is worth the extra characters. Ten bare values in a row are unreadable; labelled output tells you which line produced which value.
console.time("load") and console.timeEnd("load") measure elapsed time, which is handy before reaching for a profiler.
ReferenceError
A ReferenceError means the name does not exist in scope. The usual causes are a typo, a variable declared inside a block and used outside it, or a variable used before its declaration.
Editable. Runs in your browser.
const total = 10;
console.log(total);
Press Run to see the output.
Read the message: total is not defined. Check the spelling and the scope before changing logic. The error is telling you the name resolution failed, not that the value is wrong.
TypeError
A TypeError means the value exists but the operation does not fit: reading a property of null, calling something that is not a function, or treating a number as an object.
Editable. Runs in your browser.
try {
console.log(element.textContent);
} catch (error) {
console.log(error.name, "-", error.message);
}
Press Run to see the output.
Cannot read properties of null (reading 'textContent') is the most common browser JavaScript error, and it almost always means the selector matched nothing. Check the id or class, and check that the script runs after the element exists.
A variant is x is not a function, which happens when a method name is misspelled or a library did not load.
SyntaxError
A SyntaxError means the file could not be parsed, so nothing in it ran. A missing bracket, an unclosed string or a stray comma will do it.
The reported line is often just after the real mistake, because the parser only notices when the structure cannot continue. When the arrow points at something that looks fine, look at the previous line.
Debugging by narrowing
When the cause is not obvious:
- Read the error name and message, then the top line of the stack trace in your own file.
- Log the value just before the failing line, with a label and its type.
- Comment out or remove half the code and see whether the error moves.
- Check the smallest failing example in isolation.
The goal is to shrink the distance between "something is wrong" and "this exact value is wrong". Guessing without measuring usually adds a second bug.
Breakpoints, briefly
Open DevTools, go to Sources, find your file and click a line number. The browser pauses there, and you can inspect every variable in scope. Step over a line, step into a function, or resume.
For code that only fails on a click, a breakpoint is far more effective than adding ten logs and removing them later. debugger; in the source does the same when DevTools is open.
Throwing your own errors
throw new Error("message") stops execution and produces a stack trace. Use it when continuing would corrupt data.
Editable. Runs in your browser.
try {
console.log(parseScore("88"));
console.log(parseScore("eighty"));
} catch (error) {
console.log("Caught:", error.message);
}
Press Run to see the output.
The try/catch here keeps the page alive; module 4 covers when to catch and when to let an error surface.
Catching an error and logging only "Something went wrong" throws away the message, the type and the line. Log the error object itself, or at least error.name and error.message, so the next person — often future you — can act on it.
This code fails. Run it, read the error, then fix it and say which kind of error it was.
const button = document.querySelector("#save");
button.addEventListener("click", () => console.log("saved"));
Show solution
Editable. Runs in your browser.
console.log(button);
Press Run to see the output.
The selector returns null because no element with that id exists in this page, so addEventListener throws TypeError: Cannot read properties of null. The fix is to use an id that exists, or to guard: if (button) { ... }. In a real page, the usual cause is a typo in the selector or a script running before the element exists.
Recap
- Console methods label output, tabulate data and measure time; they do not replace user-facing messages.
- ReferenceError means a name is missing, TypeError means the value does not fit, SyntaxError means nothing ran.
- Narrow the failing code, log labelled values, and use breakpoints for event-driven bugs.
You have finished the basics. Next, you will make decisions with conditions and repeat work with loops.
Why does my console show old values after I edit a file?
Should errors be visible to users?
Progress is saved in this browser only. No account, no email.