Skip to content

JavaScript Errors and Debugging: Read Error Messages, Use the Console and Breakpoints

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Every developer writes bugs, every day. What separates a beginner from a professional is not fewer mistakes but how fast they find them. Beginners change lines at random and hope; professionals read the error, form a guess, test it and confirm the fix. This lesson teaches that method with the tools every browser already has: the error message itself, the console, and the debugger that can pause your code on any line so you can look at every value. You will also learn to throw clear errors of your own, so the next bug in your calculator explains itself.

Quick answer

Open the developer tools with F12, or Ctrl+Shift+I on Windows and Cmd+Option+I on a Mac, and read the first red error in the Console: its type (such as TypeError or ReferenceError), its message and the file and line number. Click the line to jump to it. Log values with labels, console.log({ total }), or show arrays with console.table(rows). To look closer, set a breakpoint in the Sources panel or write debugger;, then step through the code line by line and watch the values. Guard your own functions with checks that throw new Error("...") and handle expected failures with try and catch.

35 min read and practice · Content checked on 15 November 2026

In this lesson you will

  • Read an error message and stack trace and find the line that caused it
  • Tell SyntaxError, ReferenceError, TypeError and RangeError apart
  • Use console methods, breakpoints and the debugger statement to inspect values
  • Throw clear errors in your own functions and follow a step-by-step debugging method

Reading an error message

An error message has three parts, and all three matter. The type says what kind of problem it is. The message says what exactly went wrong, such as "total is not defined" or "Cannot read properties of null (reading 'value')". The location gives the file and line, for example app.js:14, and below it the stack trace lists the chain of function calls that led there, newest first. Start at the top line that points to your own code, not to a library. When there are many errors, fix the first one: later errors are often only consequences of it.

Four types cover most beginner bugs. A SyntaxError means the code cannot even be read, such as a missing bracket or quote, and nothing in that script runs. A ReferenceError means a name does not exist, usually a typo or a variable used outside its block. A TypeError means a value is the wrong kind for what you did with it: calling something that is not a function, or reading a property of null because querySelector found nothing. A RangeError means a number is outside the allowed range, such as toFixed(200). Some bugs give no error at all, only a wrong result such as NaN, and those need the tools in the next sections.

Try it: Four common errors, caught and named
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

Each message names the problem precisely: a misspelt variable, a property read from null because the element does not exist, a number out of range, and JSON without quotes around the key. In your own code you would also see the line number.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

How do I open the developer tools on a phone?

Phones have no built-in developer tools panel. Connect an Android phone to a computer by USB and open chrome://inspect in desktop Chrome, or connect an iPhone to a Mac and use the Develop menu in Safari. The device mode in desktop developer tools is a quick first check for phone layouts.

What does "Uncaught (in promise)" mean?

A promise was rejected and nothing handled it. Usually an await or a fetch is missing a surrounding try and catch, or a .then() chain has no .catch(). Find the line in the message and add the handling there.

Why does my JavaScript show NaN instead of a number?

NaN means "not a number" and appears when maths gets a value that cannot become a number, such as "12 AED" or undefined, and it spreads through every later calculation. Log each value with its type to find the first NaN, then check it with Number.isNaN() before using it.

Can I use AI to fix JavaScript bugs?

It can help explain an error message or suggest a cause, especially when you paste the exact message and the lines around it. Still read and test every suggestion yourself, because AI tools can be confidently wrong, and never paste passwords, keys or customer data into them.

What is the difference between an error and a warning in the console?

An error, in red, means something failed: an exception your code did not catch, a failed request or a console.error call. A warning, in yellow, means something works but may cause trouble, such as a deprecated feature. Fix errors first, then read the warnings.

Sources

Facts in this lesson were checked against these official pages on 15 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.