Lesson 5 of 28

In progress

Course progress

JavaScript

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

JavaScript
Editable. Runs in your browser.





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.

JavaScript
Editable. Runs in your browser.





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.

JavaScript
Editable. Runs in your browser.





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:

  1. Read the error name and message, then the top line of the stack trace in your own file.
  2. Log the value just before the failing line, with a label and its type.
  3. Comment out or remove half the code and see whether the error moves.
  4. 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.

JavaScript
Editable. Runs in your browser.





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
JavaScript
Editable. Runs in your browser.





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?
The browser cached the script or you are looking at a stale tab. Hard-reload with Ctrl+Shift+R, and check the Network panel that the file returned 200 rather than 304.
Should errors be visible to users?
Not the raw message. Log the technical detail for yourself and show a short, specific sentence in the interface, such as “Could not save. Try again.”

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.